-
Notifications
You must be signed in to change notification settings - Fork 82
/
results-tree.c
370 lines (310 loc) · 7.9 KB
/
results-tree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
* results-tree.c
*
* Copyright (C) 2013 SUSE. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* Authors: Mark Fasheh <[email protected]>
*/
#include <stdlib.h>
#include <errno.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include <linux/fiemap.h>
#include "kernel.h"
#include "rbtree.h"
#include "list.h"
#include "list.h"
#include "csum.h" /* for DIGEST_LEN */
#include "filerec.h"
#include "results-tree.h"
#include "debug.h"
#include "memstats.h"
declare_alloc_tracking(dupe_extents);
declare_alloc_tracking(extent);
static struct extent *alloc_extent(struct filerec *file, uint64_t loff)
{
struct extent *e = calloc_extent(1);
if (e) {
INIT_LIST_HEAD(&e->e_list);
rb_init_node(&e->e_node);
e->e_file = file;
e->e_loff = loff;
}
return e;
}
static int extents_rb_cmp(struct extent *e1, struct extent *e2)
{
if (e1->e_file > e2->e_file)
return -1;
if (e1->e_file < e2->e_file)
return 1;
if (e1->e_loff > e2->e_loff)
return -1;
if (e1->e_loff < e2->e_loff)
return 1;
return 0;
}
static int insert_extent_rb(struct dupe_extents *dext, struct extent *e)
{
int res;
struct extent *tmp;
struct rb_node **p = &dext->de_extents_root.rb_node;
struct rb_node *parent = NULL;
while (*p) {
parent = *p;
tmp = rb_entry(parent, struct extent, e_node);
res = extents_rb_cmp(tmp, e);
if (res < 0) {
p = &(*p)->rb_left;
} else if (res > 0) {
p = &(*p)->rb_right;
} else {
return 1;
}
}
rb_link_node(&e->e_node, parent, p);
rb_insert_color(&e->e_node, &dext->de_extents_root);
return 0;
}
static void insert_dupe_extents(struct results_tree *res,
struct dupe_extents *dext)
{
struct rb_node **p = &res->root.rb_node;
struct rb_node *parent = NULL;
struct dupe_extents *tmp;
int cmp;
while (*p) {
parent = *p;
tmp = rb_entry(parent, struct dupe_extents, de_node);
if (dext->de_len < tmp->de_len)
p = &(*p)->rb_left;
else if (dext->de_len > tmp->de_len)
p = &(*p)->rb_right;
else {
cmp = memcmp(dext->de_hash, tmp->de_hash, DIGEST_LEN);
if (cmp < 0)
p = &(*p)->rb_left;
else if (cmp > 0)
p = &(*p)->rb_right;
else
abort_lineno(); /* We should never find a duplicate */
}
}
res->num_dupes++;
rb_link_node(&dext->de_node, parent, p);
rb_insert_color(&dext->de_node, &res->root);
}
static struct dupe_extents *find_dupe_extents(struct results_tree *res,
unsigned char *digest, uint64_t len)
{
struct rb_node *n = res->root.rb_node;
struct dupe_extents *dext;
int cmp;
while (n) {
dext = rb_entry(n, struct dupe_extents, de_node);
/* Compare by length first, then use digest to drill
* down to a match */
if (len < dext->de_len)
n = n->rb_left;
else if (len > dext->de_len)
n = n->rb_right;
else {
cmp = memcmp(digest, dext->de_hash, DIGEST_LEN);
if (cmp < 0)
n = n->rb_left;
else if (cmp > 0)
n = n->rb_right;
else
return dext;
}
}
return NULL;
}
static void insert_extent_list_free(struct dupe_extents *dext,
struct extent **e)
{
/* We keep this tree free of duplicates */
if (insert_extent_rb(dext, *e) == 0) {
(*e)->e_parent = dext;
dext->de_num_dupes++;
list_add_tail(&(*e)->e_list, &dext->de_extents);
return;
}
free_extent(*e);
*e = NULL;
}
static struct dupe_extents *dupe_extents_new(unsigned char *digest,
uint64_t len)
{
struct dupe_extents *dext;
dext = calloc_dupe_extents(1);
if (!dext)
return NULL;
memcpy(dext->de_hash, digest, DIGEST_LEN);
dext->de_len = len;
dext->de_score = len;
INIT_LIST_HEAD(&dext->de_extents);
dext->de_extents_root = RB_ROOT;
rb_init_node(&dext->de_node);
g_mutex_init(&dext->de_mutex);
return dext;
}
static struct dupe_extents *find_alloc_dext(struct results_tree *res,
unsigned char *digest,
uint64_t len, int *add_score)
{
struct dupe_extents *dext, *new;
if (add_score)
*add_score = 1;
g_mutex_lock(&res->tree_mutex);
dext = find_dupe_extents(res, digest, len);
g_mutex_unlock(&res->tree_mutex);
if (!dext) {
new = dupe_extents_new(digest, len);
g_mutex_lock(&res->tree_mutex);
dext = find_dupe_extents(res, digest, len);
if (dext) {
g_mutex_unlock(&res->tree_mutex);
free_dupe_extents(new);
return dext;
}
insert_dupe_extents(res, new);
g_mutex_unlock(&res->tree_mutex);
if (add_score)
*add_score = 0;
return new;
}
return dext;
}
/*
* This does not do all the work of insert_result(), just enough for
* the dedupe phase of block-dedupe to work properly.
*/
int insert_one_result(struct results_tree *res, unsigned char *digest,
struct filerec *file, uint64_t startoff, uint64_t len,
uint64_t poff)
{
struct extent *extent = alloc_extent(file, startoff);
struct dupe_extents *dext;
if (!extent)
return ENOMEM;
extent_poff(extent) = poff;
extent_plen(extent) = len;
extent_shared_bytes(extent) = 0;
dext = find_alloc_dext(res, digest, len, NULL);
if (!dext)
return ENOMEM;
abort_on(dext->de_len != len);
g_mutex_lock(&dext->de_mutex);
insert_extent_list_free(dext, &extent);
g_mutex_unlock(&dext->de_mutex);
if (!extent)
return 0;
g_mutex_lock(&res->tree_mutex);
res->num_extents++;
g_mutex_unlock(&res->tree_mutex);
return 0;
}
int insert_result(struct results_tree *res, unsigned char *digest,
struct filerec *recs[2], uint64_t startoff[2],
uint64_t endoff[2])
{
struct extent *e0 = alloc_extent(recs[0], startoff[0]);
struct extent *e1 = alloc_extent(recs[1], startoff[1]);
struct dupe_extents *dext;
uint64_t len = endoff[0] - startoff[0] + 1;
int add_score;
if (!e0 || !e1)
return ENOMEM;
dext = find_alloc_dext(res, digest, len, &add_score);
if (!dext)
return ENOMEM;
abort_on(dext->de_len != len);
g_mutex_lock(&dext->de_mutex);
insert_extent_list_free(dext, &e0);
insert_extent_list_free(dext, &e1);
if (add_score) {
if (e0)
dext->de_score += len;
if (e1)
dext->de_score += len;
}
g_mutex_unlock(&dext->de_mutex);
g_mutex_lock(&res->tree_mutex);
if (e0)
res->num_extents++;
if (e1)
res->num_extents++;
g_mutex_unlock(&res->tree_mutex);
return 0;
}
unsigned int remove_extent(struct results_tree *res, struct extent *extent)
{
struct dupe_extents *p = extent->e_parent;
struct rb_node *n;
unsigned int result;
again:
p->de_score -= p->de_len;
p->de_num_dupes--;
result = p->de_num_dupes;
list_del_init(&extent->e_list);
rb_erase(&extent->e_node, &p->de_extents_root);
free_extent(extent);
res->num_extents--;
if (p->de_num_dupes == 1) {
/* It doesn't make sense to have one extent in a dup
* list. */
abort_on(RB_EMPTY_ROOT(&p->de_extents_root));/* logic error */
n = rb_first(&p->de_extents_root);
extent = rb_entry(n, struct extent, e_node);
goto again;
}
if (p->de_num_dupes == 0) {
rb_erase(&p->de_node, &res->root);
free_dupe_extents(p);
res->num_dupes--;
}
return result;
}
void init_results_tree(struct results_tree *res)
{
res->root = RB_ROOT;
res->num_dupes = 0;
g_mutex_init(&res->tree_mutex);
res->num_extents = 0;
}
void dupe_extents_free(struct dupe_extents *dext, struct results_tree *res)
{
struct extent *extent;
struct rb_node *n;
int count;
/*
* remove_extent will remove all stuff if there is less
* than one extent remaining
*/
do {
n = rb_first(&dext->de_extents_root);
extent = rb_entry(n, struct extent, e_node);
count = remove_extent(res, extent);
} while (count > 0);
}
void free_results_tree(struct results_tree *res)
{
struct dupe_extents *de;
struct rb_node *n = rb_first(&res->root);
while (n) {
de = rb_entry(n, struct dupe_extents, de_node);
dupe_extents_free(de, res);
n = rb_first(&res->root);
}
}