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>
28 __FBSDID("$FreeBSD$");
36 #if defined(MKUZ_DEBUG)
41 #include "mkuz_blockcache.h"
44 struct mkuz_blkcache_itm {
45 struct mkuz_blk_info hit;
46 struct mkuz_blkcache_itm *next;
49 static struct mkuz_blkcache {
50 struct mkuz_blkcache_itm first[256];
54 verify_match(int fd, const struct mkuz_blk *cbp, struct mkuz_blkcache_itm *bcep)
61 vbuf = malloc(cbp->info.len);
65 if (lseek(fd, bcep->hit.offset, SEEK_SET) < 0) {
68 rlen = read(fd, vbuf, cbp->info.len);
69 if (rlen < 0 || (unsigned)rlen != cbp->info.len) {
72 rval = (memcmp(cbp->data, vbuf, cbp->info.len) == 0) ? 1 : 0;
74 lseek(fd, cbp->info.offset, SEEK_SET);
81 #define I2J(x) ((intmax_t)(x))
82 #define U2J(x) ((uintmax_t)(x))
85 digest_fold(const unsigned char *mdigest)
91 for (i = 1; i < 16; i++) {
92 rval = rval ^ mdigest[i];
97 struct mkuz_blk_info *
98 mkuz_blkcache_regblock(int fd, const struct mkuz_blk *bp)
100 struct mkuz_blkcache_itm *bcep;
104 #if defined(MKUZ_DEBUG)
105 assert((unsigned)lseek(fd, 0, SEEK_CUR) == bp->info.offset);
107 h = digest_fold(bp->info.digest);
108 if (blkcache.first[h].hit.len == 0) {
109 bcep = &blkcache.first[h];
111 for (bcep = &blkcache.first[h]; bcep != NULL; bcep = bcep->next) {
112 if (bcep->hit.len != bp->info.len)
114 if (memcmp(bp->info.digest, bcep->hit.digest,
115 sizeof(bp->info.digest)) == 0) {
120 rval = verify_match(fd, bp, bcep);
122 #if defined(MKUZ_DEBUG)
123 fprintf(stderr, "cache hit %jd, %jd, %jd, %jd\n",
124 I2J(bcep->hit.blkno), I2J(bcep->hit.offset),
125 I2J(bp->info.offset), I2J(bp->info.len));
130 #if defined(MKUZ_DEBUG)
131 fprintf(stderr, "block MD5 collision, you should try lottery, "
136 warn("verify_match");
139 bcep = malloc(sizeof(struct mkuz_blkcache_itm));
142 memset(bcep, '\0', sizeof(struct mkuz_blkcache_itm));
143 bcep->next = blkcache.first[h].next;
144 blkcache.first[h].next = bcep;
146 bcep->hit = bp->info;