xref: /freebsd/sys/contrib/openzfs/lib/libzfs/libzfs_impl.h (revision efa8679e7f69c9cc225613827d9f75644cca5b3b)
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 	boolean_t zh_mnttab_cache_enabled;
70 };
71 
72 struct zfs_handle {
73 	libzfs_handle_t *zfs_hdl;
74 	zpool_handle_t *zpool_hdl;
75 	char zfs_name[ZFS_MAX_DATASET_NAME_LEN];
76 	zfs_type_t zfs_type; /* type including snapshot */
77 	zfs_type_t zfs_head_type; /* type excluding snapshot */
78 	dmu_objset_stats_t zfs_dmustats;
79 	nvlist_t *zfs_props;
80 	nvlist_t *zfs_user_props;
81 	nvlist_t *zfs_recvd_props;
82 	boolean_t zfs_mntcheck;
83 	char *zfs_mntopts;
84 	uint8_t *zfs_props_table;
85 };
86 
87 /*
88  * Internal namespace property flags for selective remount via
89  * mount_setattr(2).  Passed to zfs_mount_setattr().
90  */
91 #define	ZFS_MNT_PROP_ATIME	(1U << 0)
92 #define	ZFS_MNT_PROP_RELATIME	(1U << 1)
93 #define	ZFS_MNT_PROP_DEVICES	(1U << 2)
94 #define	ZFS_MNT_PROP_EXEC	(1U << 3)
95 #define	ZFS_MNT_PROP_SETUID	(1U << 4)
96 #define	ZFS_MNT_PROP_READONLY	(1U << 5)
97 #define	ZFS_MNT_PROP_XATTR	(1U << 6)
98 #define	ZFS_MNT_PROP_NBMAND	(1U << 7)
99 
100 /*
101  * This is different from checking zfs_type, because it will also catch
102  * snapshots of volumes.
103  */
104 #define	ZFS_IS_VOLUME(zhp) ((zhp)->zfs_head_type == ZFS_TYPE_VOLUME)
105 #define	ZHP_MAX_PROPNAMES 4
106 
107 struct zpool_handle {
108 	libzfs_handle_t *zpool_hdl;
109 	zpool_handle_t *zpool_next;
110 	char zpool_name[ZFS_MAX_DATASET_NAME_LEN];
111 	int zpool_state;
112 	unsigned int zpool_n_propnames;
113 	const char *zpool_propnames[ZHP_MAX_PROPNAMES];
114 	size_t zpool_config_size;
115 	nvlist_t *zpool_config;
116 	nvlist_t *zpool_old_config;
117 	nvlist_t *zpool_props;
118 	diskaddr_t zpool_start_block;
119 };
120 
121 typedef int (*zfs_uri_handler_fn_t)(struct libzfs_handle *, const char *,
122     const char *, zfs_keyformat_t, boolean_t, uint8_t **, size_t *);
123 
124 typedef struct zfs_uri_handler {
125 	const char *zuh_scheme;
126 	zfs_uri_handler_fn_t zuh_handler;
127 } zfs_uri_handler_t;
128 
129 #define	CONFIG_BUF_MINSIZE	262144
130 
131 extern int zfs_error(libzfs_handle_t *, int, const char *);
132 extern int zfs_error_fmt(libzfs_handle_t *, int, const char *, ...)
133     __attribute__((format(printf, 3, 4)));
134 extern void zfs_error_aux(libzfs_handle_t *, const char *, ...)
135     __attribute__((format(printf, 2, 3)));
136 extern void *zfs_alloc(libzfs_handle_t *, size_t);
137 extern void *zfs_realloc(libzfs_handle_t *, void *, size_t, size_t);
138 extern char *zfs_asprintf(libzfs_handle_t *, const char *, ...)
139     __attribute__((format(printf, 2, 3)));
140 extern char *zfs_strdup(libzfs_handle_t *, const char *);
141 extern int no_memory(libzfs_handle_t *);
142 
143 extern int zfs_standard_error_fmt(libzfs_handle_t *, int, const char *, ...)
144     __attribute__((format(printf, 3, 4)));
145 extern void zfs_setprop_error(libzfs_handle_t *, zfs_prop_t, int, char *);
146 extern int zpool_standard_error(libzfs_handle_t *, int, const char *);
147 extern int zpool_standard_error_fmt(libzfs_handle_t *, int, const char *, ...)
148     __attribute__((format(printf, 3, 4)));
149 
150 extern zfs_handle_t *make_dataset_handle_zc(libzfs_handle_t *, zfs_cmd_t *);
151 extern zfs_handle_t *make_dataset_simple_handle_zc(zfs_handle_t *, zfs_cmd_t *);
152 
153 extern int zprop_parse_value(libzfs_handle_t *, nvpair_t *, int, zfs_type_t,
154     nvlist_t *, const char **, uint64_t *, const char *);
155 extern int zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp,
156     zfs_type_t type);
157 
158 /*
159  * Use this changelist_gather() flag to force attempting mounts
160  * on each change node regardless of whether or not it is currently
161  * mounted.
162  */
163 #define	CL_GATHER_MOUNT_ALWAYS	1
164 /*
165  * changelist_gather() flag to force it to iterate on mounted datasets only
166  */
167 #define	CL_GATHER_ITER_MOUNTED	2
168 /*
169  * Use this changelist_gather() flag to prevent unmounting of file systems.
170  */
171 #define	CL_GATHER_DONT_UNMOUNT	4
172 
173 typedef struct prop_changelist prop_changelist_t;
174 
175 extern void zcmd_alloc_dst_nvlist(libzfs_handle_t *, zfs_cmd_t *, size_t);
176 extern void zcmd_write_src_nvlist(libzfs_handle_t *, zfs_cmd_t *, nvlist_t *);
177 extern void zcmd_write_conf_nvlist(libzfs_handle_t *, zfs_cmd_t *, nvlist_t *);
178 extern void zcmd_expand_dst_nvlist(libzfs_handle_t *, zfs_cmd_t *);
179 extern int zcmd_read_dst_nvlist(libzfs_handle_t *, zfs_cmd_t *, nvlist_t **);
180 extern void zcmd_free_nvlists(zfs_cmd_t *);
181 
182 extern int changelist_prefix(prop_changelist_t *);
183 extern int changelist_postfix(prop_changelist_t *);
184 extern void changelist_rename(prop_changelist_t *, const char *, const char *);
185 extern void changelist_remove(prop_changelist_t *, const char *);
186 extern void changelist_free(prop_changelist_t *);
187 extern prop_changelist_t *changelist_gather(zfs_handle_t *, zfs_prop_t, int,
188     int);
189 extern int changelist_unshare(prop_changelist_t *, const enum sa_protocol *);
190 extern int changelist_haszonedchild(prop_changelist_t *);
191 
192 extern boolean_t zfs_is_namespace_prop(zfs_prop_t);
193 extern uint32_t zfs_namespace_prop_flag(zfs_prop_t);
194 extern boolean_t zfs_is_mountable_internal(zfs_handle_t *);
195 extern int zfs_mount_setattr(zfs_handle_t *, uint32_t);
196 extern void remove_mountpoint(zfs_handle_t *);
197 extern int create_parents(libzfs_handle_t *, char *, int, nvlist_t *);
198 
199 extern zfs_handle_t *make_dataset_handle(libzfs_handle_t *, const char *);
200 extern zfs_handle_t *make_bookmark_handle(zfs_handle_t *, const char *,
201     nvlist_t *props);
202 
203 extern int zpool_open_silent(libzfs_handle_t *, const char *,
204     zpool_handle_t **);
205 
206 extern boolean_t zpool_name_valid(libzfs_handle_t *, boolean_t, const char *);
207 
208 extern int zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
209     boolean_t modifying);
210 
211 extern void namespace_clear(libzfs_handle_t *);
212 
213 typedef struct {
214 	zfs_prop_t p_prop;
215 	int p_share_err;
216 	int p_unshare_err;
217 } proto_table_t;
218 
219 typedef struct differ_info {
220 	zfs_handle_t *zhp;
221 	char *fromsnap;
222 	char *frommnt;
223 	char *tosnap;
224 	char *tomnt;
225 	char *ds;
226 	char *dsmnt;
227 	char *tmpsnap;
228 	char errbuf[ERRBUFLEN];
229 	boolean_t isclone;
230 	boolean_t scripted;
231 	boolean_t classify;
232 	boolean_t timestamped;
233 	boolean_t no_mangle;
234 	uint64_t shares;
235 	int zerr;
236 	int cleanupfd;
237 	int outputfd;
238 	int datafd;
239 } differ_info_t;
240 
241 extern int do_mount(zfs_handle_t *zhp, const char *mntpt, const char *opts,
242     int flags);
243 extern int do_unmount(zfs_handle_t *zhp, const char *mntpt, int flags);
244 extern int libzfs_load_module(void);
245 extern int zpool_relabel_disk(libzfs_handle_t *hdl, const char *path,
246     const char *msg);
247 extern int find_shares_object(differ_info_t *di);
248 
249 #ifdef	__cplusplus
250 }
251 #endif
252 
253 #endif	/* _LIBZFS_IMPL_H */
254