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