Welcome to mirror list, hosted at ThFree Co, Russian Federation.

file-ids.c « criu - github.com/checkpoint-restore/criu.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 772bd92cf040c6d90357a4adba36275d5e551d23 (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <signal.h>
#include <limits.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "int.h"
#include "file-ids.h"
#include "rbtree.h"
#include "kcmp-ids.h"
#include "common/compiler.h"
#include "image.h"
#include "util.h"
#include "irmap.h"
#include "files.h"

DECLARE_KCMP_TREE(fd_tree, KCMP_FILE);

#define FDID_BITS 5
#define FDID_SIZE (1 << FDID_BITS)
#define FDID_MASK (FDID_SIZE - 1)

static inline int fdid_hashfn(unsigned int s_dev, unsigned long i_ino)
{
	return (s_dev + i_ino) & FDID_MASK;
}

struct fd_id {
	int mnt_id;
	unsigned int dev;
	unsigned long ino;
	u32 id;
	struct fd_id *n;
};

static struct fd_id *fd_id_cache[FDID_SIZE];

static void fd_id_cache_one(u32 id, struct fd_parms *p)
{
	struct fd_id *fi;
	unsigned hv;

	fi = xmalloc(sizeof(*fi));
	if (fi) {
		fi->dev = p->stat.st_dev;
		fi->ino = p->stat.st_ino;
		fi->mnt_id = p->mnt_id;
		fi->id = id;

		hv = fdid_hashfn(p->stat.st_dev, p->stat.st_ino);
		fi->n = fd_id_cache[hv];
		fd_id_cache[hv] = fi;
	}
}

static struct fd_id *fd_id_cache_lookup(struct fd_parms *p)
{
	struct stat *st = &p->stat;
	struct fd_id *fi;

	for (fi = fd_id_cache[fdid_hashfn(st->st_dev, st->st_ino)]; fi; fi = fi->n)
		if (fi->dev == st->st_dev && fi->ino == st->st_ino && fi->mnt_id == p->mnt_id)
			return fi;

	return NULL;
}

int fd_id_generate_special(struct fd_parms *p, u32 *id)
{
	if (p) {
		struct fd_id *fi;

		fi = fd_id_cache_lookup(p);
		if (fi) {
			if (p->stat.st_mode & (S_IFCHR | S_IFBLK)) {
				/* Don't cache the id for mapped devices */
				*id = fd_tree.subid++;
				return 1;
			} else {
				*id = fi->id;
				return 0;
			}
		}
	}

	*id = fd_tree.subid++;
	if (p)
		fd_id_cache_one(*id, p);
	return 1;
}

int fd_id_generate(pid_t pid, FdinfoEntry *fe, struct fd_parms *p)
{
	u32 id;
	struct kid_elem e;
	int new_id = 0;

	e.pid = pid;
	e.genid = fe->id;
	e.idx = fe->fd;

	id = kid_generate_gen(&fd_tree, &e, &new_id);
	if (!id)
		return -ENOMEM;

	if (new_id)
		fd_id_cache_one(id, p);

	fe->id = id;
	return new_id;
}