Move devpath scan to separate file.
[platform/upstream/cryptsetup.git] / lib / internal.h
1 #ifndef INTERNAL_H
2 #define INTERNAL_H
3
4 #ifdef HAVE_CONFIG_H
5 #       include "config.h"
6 #endif
7
8 #include <stdint.h>
9 #include <stdarg.h>
10 #include <unistd.h>
11 #include <inttypes.h>
12
13 #include "nls.h"
14 #include "utils_crypt.h"
15 #include "utils_loop.h"
16
17 #define SECTOR_SHIFT            9
18 #define SECTOR_SIZE             (1 << SECTOR_SHIFT)
19 #define DEFAULT_DISK_ALIGNMENT  1048576 /* 1MiB */
20 #define DEFAULT_MEM_ALIGNMENT   4096
21
22 /* private struct crypt_options flags */
23
24 #define CRYPT_FLAG_FREE_DEVICE  (1 << 24)
25 #define CRYPT_FLAG_FREE_CIPHER  (1 << 25)
26
27 #define CRYPT_FLAG_PRIVATE_MASK ((unsigned int)-1 << 24)
28
29 #define at_least(a, b) ({ __typeof__(a) __at_least = (a); (__at_least >= (b))?__at_least:(b); })
30
31 struct crypt_device;
32
33 struct hash_type {
34         char            *name;
35         void            *private;
36         int             (*fn)(void *data, int size, char *key,
37                               int sizep, const char *passphrase);
38 };
39
40 struct hash_backend {
41         const char              *name;
42         struct hash_type *      (*get_hashes)(void);
43         void                    (*free_hashes)(struct hash_type *hashes);
44 };
45
46 struct volume_key {
47         size_t keylength;
48         char key[];
49 };
50
51 struct volume_key *crypt_alloc_volume_key(unsigned keylength, const char *key);
52 struct volume_key *crypt_generate_volume_key(struct crypt_device *cd, unsigned keylength);
53 void crypt_free_volume_key(struct volume_key *vk);
54
55 int crypt_confirm(struct crypt_device *cd, const char *msg);
56
57 void set_error_va(const char *fmt, va_list va);
58 void set_error(const char *fmt, ...);
59 const char *get_error(void);
60
61 /* Device mapper backend - kernel support flags */
62 #define DM_KEY_WIPE_SUPPORTED (1 << 0)  /* key wipe message */
63 #define DM_LMK_SUPPORTED      (1 << 1)  /* lmk mode */
64 #define DM_SECURE_SUPPORTED   (1 << 2)  /* wipe (secure) buffer flag */
65 #define DM_PLAIN64_SUPPORTED  (1 << 3)  /* plain64 IV */
66 uint32_t dm_flags(void);
67
68 const char *dm_get_dir(void);
69 int dm_init(struct crypt_device *context, int check_kernel);
70 void dm_exit(void);
71 int dm_remove_device(const char *name, int force, uint64_t size);
72 int dm_status_device(const char *name);
73 int dm_query_device(const char *name,
74                     char **device,
75                     uint64_t *size,
76                     uint64_t *skip,
77                     uint64_t *offset,
78                     char **cipher,
79                     int *key_size,
80                     char **key,
81                     int *read_only,
82                     int *suspended,
83                     char **uuid);
84 int dm_create_device(const char *name, const char *device, const char *cipher,
85                      const char *type, const char *uuid,
86                      uint64_t size, uint64_t skip, uint64_t offset,
87                      size_t key_size, const char *key,
88                      int read_only, int reload);
89 int dm_suspend_and_wipe_key(const char *name);
90 int dm_resume_and_reinstate_key(const char *name,
91                                 size_t key_size,
92                                 const char *key);
93 char *dm_device_path(const char *dev_id);
94 int dm_is_dm_device(int major);
95
96 char *crypt_lookup_dev(const char *dev_id);
97
98 int sector_size_for_device(const char *device);
99 int device_read_ahead(const char *dev, uint32_t *read_ahead);
100 ssize_t write_blockwise(int fd, void *buf, size_t count);
101 ssize_t read_blockwise(int fd, void *_buf, size_t count);
102 ssize_t write_lseek_blockwise(int fd, char *buf, size_t count, off_t offset);
103 int device_ready(struct crypt_device *cd, const char *device, int mode);
104 int get_device_infos(const char *device,
105                      int open_exclusive,
106                      int *readonly,
107                      uint64_t *size);
108 int device_check_and_adjust(struct crypt_device *cd,
109                             const char *device,
110                             int open_exclusive,
111                             uint64_t *size,
112                             uint64_t *offset,
113                             int *read_only);
114 int wipe_device_header(const char *device, int sectors);
115
116 void logger(struct crypt_device *cd, int class, const char *file, int line, const char *format, ...);
117 #define log_dbg(x...) logger(NULL, CRYPT_LOG_DEBUG, __FILE__, __LINE__, x)
118 #define log_std(c, x...) logger(c, CRYPT_LOG_NORMAL, __FILE__, __LINE__, x)
119 #define log_verbose(c, x...) logger(c, CRYPT_LOG_VERBOSE, __FILE__, __LINE__, x)
120 #define log_err(c, x...) do { \
121         logger(c, CRYPT_LOG_ERROR, __FILE__, __LINE__, x); \
122         set_error(x); } while(0)
123
124 int crypt_get_debug_level(void);
125 void debug_processes_using_device(const char *name);
126
127 int crypt_memlock_inc(struct crypt_device *ctx);
128 int crypt_memlock_dec(struct crypt_device *ctx);
129
130 void get_topology_alignment(const char *device,
131                             unsigned long *required_alignment, /* bytes */
132                             unsigned long *alignment_offset,   /* bytes */
133                             unsigned long default_alignment);
134
135 enum { CRYPT_RND_NORMAL = 0, CRYPT_RND_KEY = 1 };
136 int crypt_random_init(struct crypt_device *ctx);
137 int crypt_random_get(struct crypt_device *ctx, char *buf, size_t len, int quality);
138 void crypt_random_exit(void);
139 int crypt_random_default_key_rng(void);
140
141 int crypt_plain_hash(struct crypt_device *ctx,
142                      const char *hash_name,
143                      char *key, size_t key_size,
144                      const char *passphrase, size_t passphrase_size);
145
146 #endif /* INTERNAL_H */