1 #ifndef _CHUNKS_INTERNAL_H_
2 #define _CHUNKS_INTERNAL_H_
3 
4 #include <stdint.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 
9 #include "rwhashtab.h"
10 
11 /* Chunk metadata structure was allocated by malloc(3). */
12 #define	CHDATA_MALLOC	((uint32_t)(1) << 30)
13 /* Chunk belongs to the current tape. */
14 #define CHDATA_CTAPE	((uint32_t)(1) << 31)
15 #define CHDATA_FLAGS	(0xc0000000)
16 #define CHDATA_ZLEN	(~CHDATA_FLAGS)
17 
18 /* In-core chunk metadata structure. */
19 struct chunkdata {
20 	uint8_t hash[32];	/* HMAC of chunk. */
21 	uint32_t len;		/* Length of chunk. */
22 	uint32_t zlen_flags;	/* Compressed length of chunk | flags. */
23 	uint32_t nrefs;		/* Number of existing tapes using this. */
24 	uint32_t ncopies;	/* Number of copies of this chunk. */
25 };
26 
27 /* In-core chunk metadata structure used by statstape. */
28 struct chunkdata_statstape {
29 	struct chunkdata d;
30 	uint32_t ncopies_ctape;	/* Used by chunks_stats only. */
31 };
32 
33 /* Chunk statistics structure. */
34 struct chunkstats {
35 	uint64_t nchunks;	/* Number of chunks. */
36 	uint64_t s_len;		/* Total length of chunks. */
37 	uint64_t s_zlen;	/* Total compressed length of chunks. */
38 };
39 
40 /**
41  * chunks_directory_read(cachepath, dir, stats_unique, stats_all, stats_extra,
42  *     mustexist, statstape):
43  * Read stats_extra statistics (statistics on non-chunks which are stored)
44  * and the chunk directory (if present) from "${cachepath}/directory" into
45  * memory allocated and assigned to ${*dir}; and return a hash table
46  * populated with struct chunkdata records.  Populate stats_all with
47  * statistics for all the chunks listed in the directory (counting
48  * multiplicity) and populate stats_unique with statistics reflecting the
49  * unique chunks.  If ${mustexist}, error out if the directory does not exist.
50  * If ${statstape}, allocate struct chunkdata_statstape records instead.
51  */
52 RWHASHTAB * chunks_directory_read(const char *, void **,
53     struct chunkstats *, struct chunkstats *, struct chunkstats *, int, int);
54 
55 /**
56  * chunks_directory_write(cachepath, HT, stats_extra, suff):
57  * Write stats_extra statistics and the contents of the hash table ${HT} of
58  * struct chunkdata records to a new chunk directory in
59  * "${cachepath}/directory${suff}".
60  */
61 int chunks_directory_write(const char *, RWHASHTAB *, struct chunkstats *,
62     const char *);
63 
64 /**
65  * chunks_directory_exists(cachepath):
66  * Return 1 if the /directory file exists within ${cachepath}, 0 if it does
67  * not, or -1 if there is an error.
68  */
69 int chunks_directory_exists(const char *);
70 
71 /**
72  * chunks_directory_free(HT, dir):
73  * Free the hash table ${HT} of struct chunkdata records, all of its
74  * elements, and ${dir}.
75  */
76 void chunks_directory_free(RWHASHTAB *, void *);
77 
78 /**
79  * chunks_directory_commit(cachepath, osuff, nsuff):
80  * If ${cachepath}/directory${osuff} exists, move it to
81  * ${cachepath}/directory${nsuff} (replacing anything already there).
82  */
83 int chunks_directory_commit(const char *, const char *, const char *);
84 
85 /**
86  * chunks_stats_zero(stats):
87  * Zero the provided set of statistics.
88  */
89 void chunks_stats_zero(struct chunkstats *);
90 
91 /**
92  * chunks_stats_add(stats, len, zlen, copies):
93  * Adjust ${stats} for the addition of ${copies} chunks each having length
94  * ${len} and compressed length ${zlen}.
95  */
96 void chunks_stats_add(struct chunkstats *, size_t len, size_t zlen,
97     ssize_t copies);
98 
99 /**
100  * chunks_stats_addstats(to, from):
101  * Add statistics in ${from} to the statistics in ${to}, storing the result
102  * in ${to}.
103  */
104 void chunks_stats_addstats(struct chunkstats *, struct chunkstats *);
105 
106 /**
107  * chunks_stats_printheader(stream, csv):
108  * Print a header line for statistics to ${stream}, optionally in ${csv}
109  * format.
110  */
111 int chunks_stats_printheader(FILE *, int);
112 
113 /**
114  * chunks_stats_print(stream, stats, name, stats_extra, csv):
115  * Print a line with ${name} and combined statistics from ${stats} and
116  * ${stats_extra} to ${stream}, optionally in ${csv} format.
117  */
118 int chunks_stats_print(FILE *, struct chunkstats *, const char *,
119     struct chunkstats *, int);
120 
121 #endif /* !_CHUNKS_INTERNAL_H_ */
122