1 /* Cumulus: Efficient Filesystem Backup to the Cloud
2 * Copyright (C) 1998, 1999 David Mazieres (dm@uun.org)
3 * Copyright (C) 2006-2008 The Regents of the University of California
5 * This code is largely taken from LBFS, primarily the files:
6 * liblbfs/fingerprint.C (fingerprint.C,v 1.1 2001/01/29 22:49:13 benjie Exp)
7 * liblbfs/rabinpoly.h (rabinpoly.h,v 1.4 2002/01/07 21:30:21 athicha Exp)
8 * liblbfs/rabinpoly.C (rabinpoly.C,v 1.1 2001/01/29 22:49:13 benjie Exp)
9 * async/msb.h (msb.h,v 1.6 1998/12/26 18:21:51 dm Exp)
10 * async/msb.C (msb.C,v 1.4 1998/12/26 18:21:51 dm Exp)
11 * but adapted and slimmed down to fit within Cumulus.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 /* Compute incremental backups at a sub-file level by chopping files up into
29 * blocks in a content-sensitive manner (using Rabin fingerprints).
39 #include <sys/types.h>
48 // Functions/data only needed internally go in a separate namespace. Public
49 // interfaces (at the end of the file) are in the global namespace.
52 #define FINGERPRINT_PT 0xbfe6b8a5bf378d83LL
53 #define BREAKMARK_VALUE 0x78
54 #define MIN_CHUNK_SIZE 2048
55 #define MAX_CHUNK_SIZE 65535
56 #define TARGET_CHUNK_SIZE 4096
58 #define SFS_DEV_RANDOM "/dev/random"
60 #define INT64(n) n##LL
61 #define MSB64 INT64(0x8000000000000000)
63 template<class R> inline R
69 /* Highest bit set in a byte */
70 static const char bytemsb[0x100] = {
71 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
72 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
73 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7,
74 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
75 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
76 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
77 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
78 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
79 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
80 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
81 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
84 /* Find last set (most significant bit) */
85 static inline u_int fls32 (uint32_t) __attribute__ ((const));
91 return 24 + bytemsb[v>>24];
93 return 16 + bytemsb[v>>16];
96 return 8 + bytemsb[v>>8];
101 static inline u_int fls64 (u_int64_t) __attribute__ ((const));
107 return 32 + fls32 (h);
109 return fls32 ((u_int32_t) v);
113 polymod (uint64_t nh, uint64_t nl, uint64_t d)
116 int k = fls64 (d) - 1;
122 for (int i = 62; i >= 0; i--)
123 if (nh & INT64 (1) << i) {
128 for (int i = 63; i >= k; i--)
129 if (nl & INT64 (1) << i)
135 polymult (uint64_t *php, uint64_t *plp, uint64_t x, uint64_t y)
137 uint64_t ph = 0, pl = 0;
140 for (int i = 1; i < 64; i++)
141 if (x & (INT64 (1) << i)) {
152 polymmult (uint64_t x, uint64_t y, uint64_t d)
155 polymult (&h, &l, x, y);
156 return polymod (h, l, d);
161 polygcd (uint64_t x, uint64_t y)
166 x = polymod (0, x, y);
169 y = polymod (0, y, x);
174 polyirreducible (uint64_t f)
177 int m = (fls64 (f) - 1) >> 1;
178 for (int i = 0; i < m; i++) {
179 u = polymmult (u, u, f);
180 if (polygcd (f, u ^ 2) != 1)
187 polygen (u_int degree)
189 assert (degree > 0 && degree < 64);
190 uint64_t msb = INT64 (1) << degree;
191 uint64_t mask = msb - 1;
193 int rfd = open (SFS_DEV_RANDOM, O_RDONLY);
195 fprintf (stderr, "%s: %m\n", SFS_DEV_RANDOM);
199 if (read (rfd, &f, sizeof (f)) != implicit_cast<ssize_t> (sizeof (f))) {
200 fprintf (stderr, "%s: read failed\n", SFS_DEV_RANDOM);
203 f = (f & mask) | msb;
204 } while (!polyirreducible (f));
212 uint64_t T[256]; // Lookup table for mod
215 const uint64_t poly; // Actual polynomial
217 explicit rabinpoly (uint64_t poly);
218 uint64_t append8 (uint64_t p, uint8_t m) const
219 { return ((p << 8) | m) ^ T[p >> shift]; }
225 assert (poly >= 0x100);
226 int xshift = fls64 (poly) - 1;
228 uint64_t T1 = polymod (0, INT64 (1) << xshift, poly);
229 for (int j = 0; j < 256; j++)
230 T[j] = polymmult (j, T1, poly) | ((uint64_t) j << xshift);
233 rabinpoly::rabinpoly (uint64_t p)
239 class window : public rabinpoly {
244 uint64_t fingerprint;
250 window (uint64_t poly);
251 uint64_t slide8 (uint8_t m) {
252 if (++bufpos >= size)
254 uint8_t om = buf[bufpos];
256 return fingerprint = append8 (fingerprint ^ U[om], m);
260 bzero (buf, sizeof (buf));
264 window::window (uint64_t poly)
265 : rabinpoly (poly), fingerprint (0), bufpos (-1)
267 uint64_t sizeshift = 1;
268 for (int i = 1; i < size; i++)
269 sizeshift = append8 (sizeshift, 0);
270 for (int i = 0; i < 256; i++)
271 U[i] = polymmult (i, sizeshift, poly);
272 bzero (buf, sizeof (buf));
275 } // end anonymous namespace
277 /* Public interface to this module. */
278 int chunk_compute_max_num_breaks(size_t buflen)
280 return (buflen / MIN_CHUNK_SIZE) + 1;
283 int chunk_compute_breaks(const char *buf, size_t len, size_t *breakpoints)
286 window w(FINGERPRINT_PT);
290 for (pos = 0; pos < len; pos++) {
291 uint64_t sig = w.slide8(buf[pos]);
292 size_t block_len = pos - start + 1;
293 if ((sig % TARGET_CHUNK_SIZE == BREAKMARK_VALUE
294 && block_len >= MIN_CHUNK_SIZE) || block_len >= MAX_CHUNK_SIZE) {
295 breakpoints[i] = pos;
303 breakpoints[i] = len - 1;
310 string chunk_algorithm_name()
313 sprintf(buf, "%s-%d", "lbfs", TARGET_CHUNK_SIZE);