hash-tree.c 9.67 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * hash-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.
Mark Fasheh's avatar
Mark Fasheh committed
14 15
 *
 * Authors: Mark Fasheh <mfasheh@suse.de>
16 17
 */

18 19 20 21 22 23
#include <stdlib.h>
#include <errno.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>

24 25 26
#include <stdio.h>
#include <inttypes.h>

27 28 29 30 31
#include "kernel.h"
#include "rbtree.h"
#include "list.h"

#include "csum.h"	/* for digest_len variable and DIGEST_LEN_MAX */
32
#include "filerec.h"
33 34

#include "hash-tree.h"
35
#include "debug.h"
36
#include "memstats.h"
37
#include "list_sort.h"
38

39 40
declare_alloc_tracking(file_block);
declare_alloc_tracking(dupe_blocks_list);
Mark Fasheh's avatar
Mark Fasheh committed
41
declare_alloc_tracking(file_hash_head);
42

43 44
extern unsigned int blocksize;

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
/*
 * Management of filerec->block_tree rb tree. This is simple - ordered
 * by loff. So that the code in find_dupes.c can walk them in logical
 * order. We use a tree for this so that our dbfile backend is free to
 * insert blocks in any order. There's no other tree management
 * required than insert.
 */
static void insert_block_into_filerec(struct filerec *file,
				      struct file_block *block)
{
	struct rb_node **p = &file->block_tree.rb_node;
	struct rb_node *parent = NULL;
	struct file_block *tmp;

	while (*p) {
		parent = *p;

		tmp = rb_entry(parent, struct file_block, b_file_next);

		if (tmp->b_loff > block->b_loff)
			p = &(*p)->rb_left;
		else if (tmp->b_loff < block->b_loff)
			p = &(*p)->rb_right;
		else abort_lineno(); /* If need be, insert_hashed_block() should
				      * check for this (right now it doesn't) */
	}

	rb_link_node(&block->b_file_next, parent, p);
	rb_insert_color(&block->b_file_next, &file->block_tree);
}

76 77 78 79
void debug_print_block(struct file_block *e)
{
	struct filerec *f = e->b_file;

Mark Fasheh's avatar
Mark Fasheh committed
80
	printf("%s\tloff: %llu lblock: %llu flags: 0x%x\n",
81 82
	       f->filename,
	       (unsigned long long)e->b_loff,
Mark Fasheh's avatar
Mark Fasheh committed
83
	       (unsigned long long)e->b_loff / blocksize,
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
	       e->b_flags);
}

void debug_print_hash_tree(struct hash_tree *tree)
{
	struct rb_root *root = &tree->root;
	struct rb_node *node = rb_first(root);
	struct dupe_blocks_list *dups;
	struct file_block *block;
	struct list_head *p;

	if (!debug)
		return;

	dprintf("Block hash tree has %"PRIu64" hash nodes and %"PRIu64" block items\n",
		tree->num_hashes, tree->num_blocks);

	while (1) {
		if (node == NULL)
			break;

		dups = rb_entry(node, struct dupe_blocks_list, dl_node);

		dprintf("All blocks with hash: ");
		debug_print_digest(stdout, dups->dl_hash);
		dprintf("\n");

		list_for_each(p, &dups->dl_list) {
			block = list_entry(p, struct file_block, b_list);
			debug_print_block(block);
		}
		node = rb_next(node);
	}
}

119 120
struct file_hash_head *find_file_hash_head(struct dupe_blocks_list *dups,
					   struct filerec *file)
121
{
122 123
	struct rb_node *n = dups->dl_files_root.rb_node;
	struct file_hash_head *head;
124

125 126
	while (n) {
		head = rb_entry(n, struct file_hash_head, h_node);
127

128 129 130 131 132 133 134
		if (head->h_file < file)
			n = n->rb_left;
		else if (head->h_file > file)
			n = n->rb_right;
		else return head;
	}
	return NULL;
135 136
}

137 138
static void insert_file_hash_head(struct dupe_blocks_list *dups,
				  struct file_hash_head *head)
139
{
140 141 142
	struct rb_node **p = &dups->dl_files_root.rb_node;
	struct rb_node *parent = NULL;
	struct file_hash_head *tmp;
143

144 145 146 147 148 149 150 151 152 153
	while (*p) {
		parent = *p;

		tmp = rb_entry(parent, struct file_hash_head, h_node);

		if (tmp->h_file < head->h_file)
			p = &(*p)->rb_left;
		else if (tmp->h_file > head->h_file)
			p = &(*p)->rb_right;
		else abort_lineno(); /* We should never find a duplicate */
154
	}
155 156 157

	rb_link_node(&head->h_node, parent, p);
	rb_insert_color(&head->h_node, &dups->dl_files_root);
158 159
}

160 161
static int add_file_hash_head(struct dupe_blocks_list *dups,
			      struct file_block *block)
162
{
163 164
	struct filerec *file = block->b_file;
	struct file_hash_head *head = find_file_hash_head(dups, file);
165

166 167
	if (head)
		goto add;
168

Mark Fasheh's avatar
Mark Fasheh committed
169
	head = malloc_file_hash_head();
170 171
	if (!head)
		return ENOMEM;
172

173 174 175 176 177 178
	head->h_file = file;
	rb_init_node(&head->h_node);
	INIT_LIST_HEAD(&head->h_blocks);
	insert_file_hash_head(dups, head);
	dups->dl_num_files++;
add:
Mark Fasheh's avatar
Mark Fasheh committed
179
	/* This list get sorted later */
180 181 182 183 184 185 186 187
	list_add_tail(&block->b_head_list, &head->h_blocks);
	return 0;
}

static void free_one_hash_head(struct dupe_blocks_list *dups,
			       struct file_hash_head *head)
{
	rb_erase(&head->h_node, &dups->dl_files_root);
Mark Fasheh's avatar
Mark Fasheh committed
188
	free_file_hash_head(head);
189 190
}

Mark Fasheh's avatar
Mark Fasheh committed
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
static int cmp_blocks(void *priv, struct list_head *a, struct list_head *b)
{
	struct file_block *fba, *fbb;

	fba = list_entry(a, struct file_block, b_head_list);
	fbb = list_entry(b, struct file_block, b_head_list);

	if (fba->b_loff < fbb->b_loff)
		return -1;
	else if (fba->b_loff > fbb->b_loff)
		return 1;
	return 0;
}

void sort_file_hash_heads(struct hash_tree *tree)
{
	struct rb_node *dups_node;
	struct dupe_blocks_list *dups;
	struct rb_node *head_node;
	struct file_hash_head *head;

	for (dups_node = rb_first(&tree->root); dups_node;
	     dups_node = rb_next(dups_node)) {
		dups = rb_entry(dups_node, struct dupe_blocks_list, dl_node);

		for (head_node = rb_first(&dups->dl_files_root); head_node;
		     head_node = rb_next(head_node)) {
			head = rb_entry(head_node, struct file_hash_head,
					h_node);
			list_sort(NULL, &head->h_blocks, cmp_blocks);
		}
	}
}

225 226 227 228 229
int file_in_dups_list(struct dupe_blocks_list *dups, struct filerec *file)
{
	if (find_file_hash_head(dups, file))
		return 1;
	return 0;
230
}
231

232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
static void insert_block_list(struct hash_tree *tree,
			      struct dupe_blocks_list *list)
{
	struct rb_node **p = &tree->root.rb_node;
	struct rb_node *parent = NULL;
	struct dupe_blocks_list *tmp;
	int cmp;

	while (*p) {
		parent = *p;

		tmp = rb_entry(parent, struct dupe_blocks_list, dl_node);

		cmp = memcmp(list->dl_hash, tmp->dl_hash, digest_len);
		if (cmp < 0)
			p = &(*p)->rb_left;
		else if (cmp > 0)
			p = &(*p)->rb_right;
250
		else abort_lineno(); /* We should never find a duplicate */
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
	}

	rb_link_node(&list->dl_node, parent, p);
	rb_insert_color(&list->dl_node, &tree->root);

	tree->num_hashes++;
	return;
}

static struct dupe_blocks_list *find_block_list(struct hash_tree *tree,
					       unsigned char *digest)
{
	struct rb_node *n = tree->root.rb_node;
	struct dupe_blocks_list *list;
	int cmp;

	while (n) {
		list = rb_entry(n, struct dupe_blocks_list, dl_node);

		cmp = memcmp(digest, list->dl_hash, digest_len);
		if (cmp < 0)
			n = n->rb_left;
		else if (cmp > 0)
			n = n->rb_right;
		else return list;
	}
	return NULL;
}

280
int insert_hashed_block(struct hash_tree *tree,	unsigned char *digest,
281
			struct filerec *file, uint64_t loff, unsigned int flags)
282
{
283
	struct file_block *e = malloc_file_block();
284 285 286
	struct dupe_blocks_list *d;

	if (!e)
287
		return ENOMEM;
288

289 290
	abort_on((flags & FILE_BLOCK_PARTIAL) && !(file->size % blocksize));

291 292
	d = find_block_list(tree, digest);
	if (d == NULL) {
293
		d = calloc_dupe_blocks_list(1);
294
		if (!d) {
295
			free_file_block(e);
296 297
			return ENOMEM;
		}
298 299 300 301

		memcpy(d->dl_hash, digest, digest_len);
		rb_init_node(&d->dl_node);
		INIT_LIST_HEAD(&d->dl_list);
302
		INIT_LIST_HEAD(&d->dl_size_list);
303
		d->dl_files_root = RB_ROOT;
304 305 306 307

		insert_block_list(tree, d);
	}

308
	e->b_file = file;
309
	e->b_loff = loff;
310
	e->b_flags = flags;
311 312
	e->b_parent = d;

313
	rb_init_node(&e->b_file_next);
314 315 316 317 318
	INIT_LIST_HEAD(&e->b_head_list);

	if (add_file_hash_head(d, e)) {
		free_file_block(e);
		return ENOMEM;
319 320
	}

321
	insert_block_into_filerec(file, e);
322 323
	file->num_blocks++;

324
	d->dl_num_elem++;
325 326
	if (d->dl_num_elem > 1 && list_empty(&d->dl_size_list))
		list_add(&d->dl_size_list, &tree->size_list);
327 328 329
	list_add_tail(&e->b_list, &d->dl_list);

	tree->num_blocks++;
330
	return 0;
331 332
}

333 334
int remove_hashed_block(struct hash_tree *tree,
			struct file_block *block)
335
{
336
	int ret = 0;
337
	struct dupe_blocks_list *blocklist = block->b_parent;
338
	struct file_hash_head *head;
339
	struct filerec *file = block->b_file;
340

341
	abort_on(blocklist->dl_num_elem == 0);
342

343
	if (!RB_EMPTY_NODE(&block->b_file_next)) {
344 345 346 347
		abort_on(file->num_blocks == 0);
		file->num_blocks--;
	}

348
	rb_erase(&block->b_file_next, &file->block_tree);
349 350
	list_del(&block->b_list);

351 352 353 354 355
	list_del(&block->b_head_list);
	head = find_file_hash_head(blocklist, file);
	if (head && list_empty(&head->h_blocks))
		free_one_hash_head(blocklist, head);

356
	abort_on(blocklist->dl_num_elem == 0);
357 358 359
	blocklist->dl_num_elem--;
	if (blocklist->dl_num_elem == 0) {
		rb_erase(&blocklist->dl_node, &tree->root);
360
		list_del(&blocklist->dl_size_list);
361 362
		tree->num_hashes--;

363
		free_dupe_blocks_list(blocklist);
364
		ret = 1;
365 366
	}

367
	free_file_block(block);
368
	tree->num_blocks--;
369 370 371
	return ret;
}

372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
static int cmp_by_size(void *priv, struct list_head *a, struct list_head *b)
{
	struct dupe_blocks_list *dla, *dlb;

	dla = list_entry(a, struct dupe_blocks_list, dl_size_list);
	dlb = list_entry(b, struct dupe_blocks_list, dl_size_list);

	/* largest first */
	if (dla->dl_num_elem > dlb->dl_num_elem)
		return -1;
	else if (dla->dl_num_elem < dlb->dl_num_elem)
		return 1;
	return 0;
}

void sort_hashes_by_size(struct hash_tree *tree)
{
	list_sort(tree, &tree->size_list, cmp_by_size);
}

392 393 394 395
void init_hash_tree(struct hash_tree *tree)
{
	tree->root = RB_ROOT;
	tree->num_blocks = tree->num_hashes = 0;
396
	INIT_LIST_HEAD(&tree->size_list);
397
}
398 399 400 401 402 403 404 405 406 407 408

void free_hash_tree(struct hash_tree *tree)
{
	struct dupe_blocks_list *dups;
	struct file_block *block, *tmp;
	struct rb_node *n = rb_first(&tree->root);

	while (n) {
		dups = rb_entry(n, struct dupe_blocks_list, dl_node);

		list_for_each_entry_safe(block, tmp, &dups->dl_list, b_list) {
409
			if (remove_hashed_block(tree, block))
410 411 412 413 414 415
				break;
		}

		n = rb_first(&tree->root);
	}
}