xref: /freebsd/sys/contrib/openzfs/lib/libzfs/libzfs_impl.h (revision 22649d4dba730d46244fd2dff4fd174903c8379f)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * This file and its contents are supplied under the terms of the
4  * Common Development and Distribution License ("CDDL"), version 1.0.
5  * You may only use this file in accordance with the terms of version
6  * 1.0 of the CDDL.
7  *
8  * A full copy of the text of the CDDL should have accompanied this
9  * source.  A copy of the CDDL is also available via the Internet at
10  * https://opensource.org/license/CDDL-1.0.
11  */
12 
13 /*
14  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
15  * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
16  * Copyright (c) 2018 Datto Inc.
17  * Copyright 2020 Joyent, Inc.
18  */
19 
20 #ifndef	_LIBZFS_IMPL_H
21 #define	_LIBZFS_IMPL_H
22 
23 #include <sys/fs/zfs.h>
24 #include <sys/nvpair.h>
25 #include <sys/dmu.h>
26 #include <sys/zfs_ioctl.h>
27 #include <sys/mutex.h>
28 #include <regex.h>
29 
30 #include <libzfs.h>
31 #include <libzfs_core.h>
32 
33 #include "libzfs_share.h"
34 
35 #ifdef	__cplusplus
36 extern "C" {
37 #endif
38 
39 #define	ERRBUFLEN 1024
40 
41 struct libzfs_handle {
42 	int libzfs_error;
43 	int libzfs_fd;
44 	zpool_handle_t *libzfs_pool_handles;
45 	avl_tree_t libzfs_ns_avl;
46 	uint64_t libzfs_ns_gen;
47 	int libzfs_desc_active;
48 	char libzfs_action[1024];
49 	char libzfs_desc[1024];
50 	int libzfs_printerr;
51 	int libzfs_pool_iter;
52 	boolean_t libzfs_prop_debug;
53 	regex_t libzfs_urire;
54 	uint64_t libzfs_max_nvlist;
55 	void *libfetch;
56 	char *libfetch_load_error;
57 	kmutex_t zh_mnttab_lock;
58 	avl_tree_t zh_mnttab;
59 	boolean_t zh_mnttab_cache_enabled;
60 };
61 
62 struct zfs_handle {
63 	libzfs_handle_t *zfs_hdl;
64 	zpool_handle_t *zpool_hdl;
65 	char zfs_name[ZFS_MAX_DATASET_NAME_LEN];
66 	zfs_type_t zfs_type; /* type including snapshot */
67 	zfs_type_t zfs_head_type; /* type excluding snapshot */
68 	dmu_objset_stats_t zfs_dmustats;
69 	nvlist_t *zfs_props;
70 	nvlist_t *zfs_user_props;
71 	nvlist_t *zfs_recvd_props;
72 	boolean_t zfs_mntcheck;
73 	char *zfs_mntopts;
74 	uint8_t *zfs_props_table;
75 };
76 
77 /*
78  * Internal namespace property flags for selective remount via
79  * mount_setattr(2).  Passed to zfs_mount_setattr().
80  */
81 #define	ZFS_MNT_PROP_ATIME	(1U << 0)
82 #define	ZFS_MNT_PROP_RELATIME	(1U << 1)
83 #define	ZFS_MNT_PROP_DEVICES	(1U << 2)
84 #define	ZFS_MNT_PROP_EXEC	(1U << 3)
85 #define	ZFS_MNT_PROP_SETUID	(1U << 4)
86 #define	ZFS_MNT_PROP_READONLY	(1U << 5)
87 #define	ZFS_MNT_PROP_XATTR	(1U << 6)
88 #define	ZFS_MNT_PROP_NBMAND	(1U << 7)
89 
90 /*
91  * This is different from checking zfs_type, because it will also catch
92  * snapshots of volumes.
93  */
94 #define	ZFS_IS_VOLUME(zhp) ((zhp)->zfs_head_type == ZFS_TYPE_VOLUME)
95 #define	ZHP_MAX_PROPNAMES 4
96 
97 struct zpool_handle {
98 	libzfs_handle_t *zpool_hdl;
99 	zpool_handle_t *zpool_next;
100 	char zpool_name[ZFS_MAX_DATASET_NAME_LEN];
101 	int zpool_state;
102 	unsigned int zpool_n_propnames;
103 	const char *zpool_propnames[ZHP_MAX_PROPNAMES];
104 	size_t zpool_config_size;
105 	nvlist_t *zpool_config;
106 	nvlist_t *zpool_old_config;
107 	nvlist_t *zpool_props;
108 	diskaddr_t zpool_start_block;
109 };
110 
111 typedef int (*zfs_uri_handler_fn_t)(struct libzfs_handle *, const char *,
112     const char *, zfs_keyformat_t, boolean_t, uint8_t **, size_t *);
113 
114 typedef struct zfs_uri_handler {
115 	const char *zuh_scheme;
116 	zfs_uri_handler_fn_t zuh_handler;
117 } zfs_uri_handler_t;
118 
119 #define	CONFIG_BUF_MINSIZE	262144
120 
121 extern int zfs_error(libzfs_handle_t *, int, const char *);
122 extern int zfs_error_fmt(libzfs_handle_t *, int, const char *, ...)
123     __attribute__((format(printf, 3, 4)));
124 extern void zfs_error_aux(libzfs_handle_t *, const char *, ...)
125     __attribute__((format(printf, 2, 3)));
126 extern void *zfs_alloc(libzfs_handle_t *, size_t);
127 extern void *zfs_realloc(libzfs_handle_t *, void *, size_t, size_t);
128 extern char *zfs_asprintf(libzfs_handle_t *, const char *, ...)
129     __attribute__((format(printf, 2, 3)));
130 extern char *zfs_strdup(libzfs_handle_t *, const char *);
131 extern int no_memory(libzfs_handle_t *);
132 
133 extern int zfs_standard_error_fmt(libzfs_handle_t *, int, const char *, ...)
134     __attribute__((format(printf, 3, 4)));
135 extern void zfs_setprop_error(libzfs_handle_t *, zfs_prop_t, int, char *);
136 extern int zpool_standard_error(libzfs_handle_t *, int, const char *);
137 extern int zpool_standard_error_fmt(libzfs_handle_t *, int, const char *, ...)
138     __attribute__((format(printf, 3, 4)));
139 
140 extern zfs_handle_t *make_dataset_handle_zc(libzfs_handle_t *, zfs_cmd_t *);
141 extern zfs_handle_t *make_dataset_simple_handle_zc(zfs_handle_t *, zfs_cmd_t *);
142 
143 extern int zprop_parse_value(libzfs_handle_t *, nvpair_t *, int, zfs_type_t,
144     nvlist_t *, const char **, uint64_t *, const char *);
145 extern int zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp,
146     zfs_type_t type);
147 
148 /*
149  * Use this changelist_gather() flag to force attempting mounts
150  * on each change node regardless of whether or not it is currently
151  * mounted.
152  */
153 #define	CL_GATHER_MOUNT_ALWAYS	1
154 /*
155  * changelist_gather() flag to force it to iterate on mounted datasets only
156  */
157 #define	CL_GATHER_ITER_MOUNTED	2
158 /*
159  * Use this changelist_gather() flag to prevent unmounting of file systems.
160  */
161 #define	CL_GATHER_DONT_UNMOUNT	4
162 
163 typedef struct prop_changelist prop_changelist_t;
164 
165 extern void zcmd_alloc_dst_nvlist(libzfs_handle_t *, zfs_cmd_t *, size_t);
166 extern void zcmd_write_src_nvlist(libzfs_handle_t *, zfs_cmd_t *, nvlist_t *);
167 extern void zcmd_write_conf_nvlist(libzfs_handle_t *, zfs_cmd_t *, nvlist_t *);
168 extern void zcmd_expand_dst_nvlist(libzfs_handle_t *, zfs_cmd_t *);
169 extern int zcmd_read_dst_nvlist(libzfs_handle_t *, zfs_cmd_t *, nvlist_t **);
170 extern void zcmd_free_nvlists(zfs_cmd_t *);
171 
172 extern int changelist_prefix(prop_changelist_t *);
173 extern int changelist_postfix(prop_changelist_t *);
174 extern void changelist_rename(prop_changelist_t *, const char *, const char *);
175 extern void changelist_remove(prop_changelist_t *, const char *);
176 extern void changelist_free(prop_changelist_t *);
177 extern prop_changelist_t *changelist_gather(zfs_handle_t *, zfs_prop_t, int,
178     int);
179 extern int changelist_unshare(prop_changelist_t *, const enum sa_protocol *);
180 extern int changelist_haszonedchild(prop_changelist_t *);
181 
182 extern boolean_t zfs_is_namespace_prop(zfs_prop_t);
183 extern uint32_t zfs_namespace_prop_flag(zfs_prop_t);
184 extern boolean_t zfs_is_mountable_internal(zfs_handle_t *);
185 extern int zfs_mount_setattr(zfs_handle_t *, uint32_t);
186 extern void remove_mountpoint(zfs_handle_t *);
187 extern int create_parents(libzfs_handle_t *, char *, int, nvlist_t *);
188 
189 extern zfs_handle_t *make_dataset_handle(libzfs_handle_t *, const char *);
190 extern zfs_handle_t *make_bookmark_handle(zfs_handle_t *, const char *,
191     nvlist_t *props);
192 
193 extern int zpool_open_silent(libzfs_handle_t *, const char *,
194     zpool_handle_t **);
195 
196 extern boolean_t zpool_name_valid(libzfs_handle_t *, boolean_t, const char *);
197 
198 extern int zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
199     boolean_t modifying);
200 
201 extern void namespace_clear(libzfs_handle_t *);
202 
203 typedef struct {
204 	zfs_prop_t p_prop;
205 	int p_share_err;
206 	int p_unshare_err;
207 } proto_table_t;
208 
209 typedef struct differ_info {
210 	zfs_handle_t *zhp;
211 	char *fromsnap;
212 	char *frommnt;
213 	char *tosnap;
214 	char *tomnt;
215 	char *ds;
216 	char *dsmnt;
217 	char *tmpsnap;
218 	char errbuf[ERRBUFLEN];
219 	boolean_t isclone;
220 	boolean_t scripted;
221 	boolean_t classify;
222 	boolean_t timestamped;
223 	boolean_t no_mangle;
224 	uint64_t shares;
225 	int zerr;
226 	int cleanupfd;
227 	int outputfd;
228 	int datafd;
229 } differ_info_t;
230 
231 extern int do_mount(zfs_handle_t *zhp, const char *mntpt, const char *opts,
232     int flags);
233 extern int do_unmount(zfs_handle_t *zhp, const char *mntpt, int flags);
234 extern int libzfs_load_module(void);
235 extern int zpool_relabel_disk(libzfs_handle_t *hdl, const char *path,
236     const char *msg);
237 extern int find_shares_object(differ_info_t *di);
238 
239 #ifdef	__cplusplus
240 }
241 #endif
242 
243 #endif	/* _LIBZFS_IMPL_H */
244