1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
25  * Copyright (c) 2018 Datto Inc.
26  * Copyright 2020 Joyent, Inc.
27  */
28 
29 #ifndef	_LIBZFS_IMPL_H
30 #define	_LIBZFS_IMPL_H
31 
32 #include <sys/fs/zfs.h>
33 #include <sys/spa.h>
34 #include <sys/nvpair.h>
35 #include <sys/dmu.h>
36 #include <sys/zfs_ioctl.h>
37 #include <regex.h>
38 
39 #include <libuutil.h>
40 #include <libzfs.h>
41 #include <libshare.h>
42 #include <libzfs_core.h>
43 
44 #ifdef	__cplusplus
45 extern "C" {
46 #endif
47 
48 struct libzfs_handle {
49 	int libzfs_error;
50 	int libzfs_fd;
51 	FILE *libzfs_mnttab;
52 	zpool_handle_t *libzfs_pool_handles;
53 	uu_avl_pool_t *libzfs_ns_avlpool;
54 	uu_avl_t *libzfs_ns_avl;
55 	uint64_t libzfs_ns_gen;
56 	int libzfs_desc_active;
57 	char libzfs_action[1024];
58 	char libzfs_desc[1024];
59 	int libzfs_printerr;
60 	int libzfs_storeerr; /* stuff error messages into buffer */
61 	boolean_t libzfs_mnttab_enable;
62 	/*
63 	 * We need a lock to handle the case where parallel mount
64 	 * threads are populating the mnttab cache simultaneously. The
65 	 * lock only protects the integrity of the avl tree, and does
66 	 * not protect the contents of the mnttab entries themselves.
67 	 */
68 	pthread_mutex_t libzfs_mnttab_cache_lock;
69 	avl_tree_t libzfs_mnttab_cache;
70 	int libzfs_pool_iter;
71 	char libzfs_chassis_id[256];
72 	boolean_t libzfs_prop_debug;
73 	regex_t libzfs_urire;
74 	uint64_t libzfs_max_nvlist;
75 	void *libfetch;
76 	char *libfetch_load_error;
77 };
78 
79 struct zfs_handle {
80 	libzfs_handle_t *zfs_hdl;
81 	zpool_handle_t *zpool_hdl;
82 	char zfs_name[ZFS_MAX_DATASET_NAME_LEN];
83 	zfs_type_t zfs_type; /* type including snapshot */
84 	zfs_type_t zfs_head_type; /* type excluding snapshot */
85 	dmu_objset_stats_t zfs_dmustats;
86 	nvlist_t *zfs_props;
87 	nvlist_t *zfs_user_props;
88 	nvlist_t *zfs_recvd_props;
89 	boolean_t zfs_mntcheck;
90 	char *zfs_mntopts;
91 	uint8_t *zfs_props_table;
92 };
93 
94 /*
95  * This is different from checking zfs_type, because it will also catch
96  * snapshots of volumes.
97  */
98 #define	ZFS_IS_VOLUME(zhp) ((zhp)->zfs_head_type == ZFS_TYPE_VOLUME)
99 
100 struct zpool_handle {
101 	libzfs_handle_t *zpool_hdl;
102 	zpool_handle_t *zpool_next;
103 	char zpool_name[ZFS_MAX_DATASET_NAME_LEN];
104 	int zpool_state;
105 	size_t zpool_config_size;
106 	nvlist_t *zpool_config;
107 	nvlist_t *zpool_old_config;
108 	nvlist_t *zpool_props;
109 	diskaddr_t zpool_start_block;
110 };
111 
112 typedef enum {
113 	PROTO_NFS = 0,
114 	PROTO_SMB = 1,
115 	PROTO_END = 2
116 } zfs_share_proto_t;
117 
118 /*
119  * The following can be used as a bitmask and any new values
120  * added must preserve that capability.
121  */
122 typedef enum {
123 	SHARED_NOT_SHARED = 0x0,
124 	SHARED_NFS = 0x2,
125 	SHARED_SMB = 0x4
126 } zfs_share_type_t;
127 
128 typedef int (*zfs_uri_handler_fn_t)(struct libzfs_handle *, const char *,
129     const char *, zfs_keyformat_t, boolean_t, uint8_t **, size_t *);
130 
131 typedef struct zfs_uri_handler {
132 	const char *zuh_scheme;
133 	zfs_uri_handler_fn_t zuh_handler;
134 } zfs_uri_handler_t;
135 
136 #define	CONFIG_BUF_MINSIZE	262144
137 
138 int zfs_error(libzfs_handle_t *, int, const char *);
139 int zfs_error_fmt(libzfs_handle_t *, int, const char *, ...);
140 void zfs_error_aux(libzfs_handle_t *, const char *, ...);
141 void *zfs_alloc(libzfs_handle_t *, size_t);
142 void *zfs_realloc(libzfs_handle_t *, void *, size_t, size_t);
143 char *zfs_asprintf(libzfs_handle_t *, const char *, ...);
144 char *zfs_strdup(libzfs_handle_t *, const char *);
145 int no_memory(libzfs_handle_t *);
146 
147 int zfs_standard_error(libzfs_handle_t *, int, const char *);
148 int zfs_standard_error_fmt(libzfs_handle_t *, int, const char *, ...);
149 void zfs_setprop_error(libzfs_handle_t *, zfs_prop_t, int, char *);
150 int zpool_standard_error(libzfs_handle_t *, int, const char *);
151 int zpool_standard_error_fmt(libzfs_handle_t *, int, const char *, ...);
152 
153 zfs_handle_t *make_dataset_handle_zc(libzfs_handle_t *, zfs_cmd_t *);
154 zfs_handle_t *make_dataset_simple_handle_zc(zfs_handle_t *, zfs_cmd_t *);
155 
156 int zprop_parse_value(libzfs_handle_t *, nvpair_t *, int, zfs_type_t,
157     nvlist_t *, char **, uint64_t *, const char *);
158 int zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp,
159     zfs_type_t type);
160 
161 /*
162  * Use this changelist_gather() flag to force attempting mounts
163  * on each change node regardless of whether or not it is currently
164  * mounted.
165  */
166 #define	CL_GATHER_MOUNT_ALWAYS	1
167 /*
168  * changelist_gather() flag to force it to iterate on mounted datasets only
169  */
170 #define	CL_GATHER_ITER_MOUNTED	2
171 /*
172  * Use this changelist_gather() flag to prevent unmounting of file systems.
173  */
174 #define	CL_GATHER_DONT_UNMOUNT	4
175 
176 typedef struct prop_changelist prop_changelist_t;
177 
178 int zcmd_alloc_dst_nvlist(libzfs_handle_t *, zfs_cmd_t *, size_t);
179 int zcmd_write_src_nvlist(libzfs_handle_t *, zfs_cmd_t *, nvlist_t *);
180 int zcmd_write_conf_nvlist(libzfs_handle_t *, zfs_cmd_t *, nvlist_t *);
181 int zcmd_expand_dst_nvlist(libzfs_handle_t *, zfs_cmd_t *);
182 int zcmd_read_dst_nvlist(libzfs_handle_t *, zfs_cmd_t *, nvlist_t **);
183 void zcmd_free_nvlists(zfs_cmd_t *);
184 
185 int changelist_prefix(prop_changelist_t *);
186 int changelist_postfix(prop_changelist_t *);
187 void changelist_rename(prop_changelist_t *, const char *, const char *);
188 void changelist_remove(prop_changelist_t *, const char *);
189 void changelist_free(prop_changelist_t *);
190 prop_changelist_t *changelist_gather(zfs_handle_t *, zfs_prop_t, int, int);
191 int changelist_unshare(prop_changelist_t *, zfs_share_proto_t *);
192 int changelist_haszonedchild(prop_changelist_t *);
193 
194 void remove_mountpoint(zfs_handle_t *);
195 int create_parents(libzfs_handle_t *, char *, int);
196 boolean_t isa_child_of(const char *dataset, const char *parent);
197 
198 zfs_handle_t *make_dataset_handle(libzfs_handle_t *, const char *);
199 zfs_handle_t *make_bookmark_handle(zfs_handle_t *, const char *,
200     nvlist_t *props);
201 
202 int zpool_open_silent(libzfs_handle_t *, const char *, zpool_handle_t **);
203 
204 boolean_t zpool_name_valid(libzfs_handle_t *, boolean_t, const char *);
205 
206 int zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
207     boolean_t modifying);
208 
209 void namespace_clear(libzfs_handle_t *);
210 
211 extern int zfs_parse_options(char *, zfs_share_proto_t);
212 
213 extern int zfs_unshare_proto(zfs_handle_t *,
214     const char *, zfs_share_proto_t *);
215 
216 typedef struct {
217 	zfs_prop_t p_prop;
218 	char *p_name;
219 	int p_share_err;
220 	int p_unshare_err;
221 } proto_table_t;
222 
223 typedef struct differ_info {
224 	zfs_handle_t *zhp;
225 	char *fromsnap;
226 	char *frommnt;
227 	char *tosnap;
228 	char *tomnt;
229 	char *ds;
230 	char *dsmnt;
231 	char *tmpsnap;
232 	char errbuf[1024];
233 	boolean_t isclone;
234 	boolean_t scripted;
235 	boolean_t classify;
236 	boolean_t timestamped;
237 	uint64_t shares;
238 	int zerr;
239 	int cleanupfd;
240 	int outputfd;
241 	int datafd;
242 } differ_info_t;
243 
244 extern proto_table_t proto_table[PROTO_END];
245 
246 extern int do_mount(zfs_handle_t *zhp, const char *mntpt, char *opts,
247     int flags);
248 extern int do_unmount(const char *mntpt, int flags);
249 extern int zfs_mount_delegation_check(void);
250 extern int zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto);
251 extern int unshare_one(libzfs_handle_t *hdl, const char *name,
252     const char *mountpoint, zfs_share_proto_t proto);
253 extern boolean_t zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
254     zprop_source_t *source, int flags);
255 extern zfs_share_type_t is_shared(const char *mountpoint,
256     zfs_share_proto_t proto);
257 extern int libzfs_load_module(void);
258 extern int zpool_relabel_disk(libzfs_handle_t *hdl, const char *path,
259     const char *msg);
260 extern int find_shares_object(differ_info_t *di);
261 extern void libzfs_set_pipe_max(int infd);
262 extern void zfs_commit_proto(zfs_share_proto_t *);
263 
264 #ifdef	__cplusplus
265 }
266 #endif
267 
268 #endif	/* _LIBZFS_IMPL_H */
269