1 /*
2  * Copyright © 2020 Valve Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 /* This is a basic c implementation of a fossilize db like format intended for
25  * use with the Mesa shader cache.
26  *
27  * The format is compatible enough to allow the fossilize db tools to be used
28  * to do things like merge db collections, but unlike fossilize db which uses
29  * a zlib implementation for compression of data entries, we use zstd for
30  * compression.
31  */
32 
33 #ifndef FOSSILIZE_DB_H
34 #define FOSSILIZE_DB_H
35 
36 #ifdef HAVE_FLOCK
37 #define FOZ_DB_UTIL 1
38 #endif
39 
40 #ifdef HAVE_SYS_INOTIFY_H
41 #define FOZ_DB_UTIL_DYNAMIC_LIST 1
42 #endif
43 
44 #include <stdbool.h>
45 #include <stdint.h>
46 #include <stdio.h>
47 
48 #include "simple_mtx.h"
49 
50 /* Max number of DBs our implementation can read from at once */
51 #define FOZ_MAX_DBS 9 /* Default DB + 8 Read only DBs */
52 
53 #define FOSSILIZE_BLOB_HASH_LENGTH 40
54 
55 enum {
56    FOSSILIZE_COMPRESSION_NONE = 1,
57    FOSSILIZE_COMPRESSION_DEFLATE = 2
58 };
59 
60 enum {
61    FOSSILIZE_FORMAT_VERSION = 6,
62    FOSSILIZE_FORMAT_MIN_COMPAT_VERSION = 5
63 };
64 
65 struct foz_payload_header {
66    uint32_t payload_size;
67    uint32_t format;
68    uint32_t crc;
69    uint32_t uncompressed_size;
70 };
71 
72 struct foz_db_entry {
73    uint8_t file_idx;
74    uint8_t key[20];
75    uint64_t offset;
76    struct foz_payload_header header;
77 };
78 
79 struct foz_dbs_list_updater {
80    int inotify_fd;
81    int inotify_wd; /* watch descriptor */
82    const char *list_filename;
83    thrd_t thrd;
84 };
85 
86 struct foz_db {
87    FILE *file[FOZ_MAX_DBS];          /* An array of all foz dbs */
88    FILE *db_idx;                     /* The default writable foz db idx */
89    simple_mtx_t mtx;                 /* Mutex for file/hash table read/writes */
90    simple_mtx_t flock_mtx;           /* Mutex for flocking the file for writes */
91    void *mem_ctx;
92    struct hash_table_u64 *index_db;  /* Hash table of all foz db entries */
93    bool alive;
94    const char *cache_path;
95    struct foz_dbs_list_updater updater;
96 };
97 
98 bool
99 foz_prepare(struct foz_db *foz_db, char *cache_path);
100 
101 void
102 foz_destroy(struct foz_db *foz_db);
103 
104 void *
105 foz_read_entry(struct foz_db *foz_db, const uint8_t *cache_key_160bit,
106                size_t *size);
107 
108 bool
109 foz_write_entry(struct foz_db *foz_db, const uint8_t *cache_key_160bit,
110                 const void *blob, size_t size);
111 
112 #endif /* FOSSILIZE_DB_H */
113