xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/zfs_ioctl.h (revision 5328fc53d11d7151861fa272e4fb0248b8f0e145)
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  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
24  * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
25  * Copyright 2016 RackTop Systems.
26  * Copyright (c) 2014 Integros [integros.com]
27  */
28 
29 #ifndef	_SYS_ZFS_IOCTL_H
30 #define	_SYS_ZFS_IOCTL_H
31 
32 #include <sys/cred.h>
33 #include <sys/dmu.h>
34 #include <sys/zio.h>
35 #include <sys/dsl_deleg.h>
36 #include <sys/spa.h>
37 #include <sys/zfs_stat.h>
38 
39 #ifdef _KERNEL
40 #include <sys/nvpair.h>
41 #endif	/* _KERNEL */
42 
43 #ifdef	__cplusplus
44 extern "C" {
45 #endif
46 
47 /*
48  * The structures in this file are passed between userland and the
49  * kernel.  Userland may be running a 32-bit process, while the kernel
50  * is 64-bit.  Therefore, these structures need to compile the same in
51  * 32-bit and 64-bit.  This means not using type "long", and adding
52  * explicit padding so that the 32-bit structure will not be packed more
53  * tightly than the 64-bit structure (which requires 64-bit alignment).
54  */
55 
56 /*
57  * Property values for snapdir
58  */
59 #define	ZFS_SNAPDIR_HIDDEN		0
60 #define	ZFS_SNAPDIR_VISIBLE		1
61 
62 /*
63  * Field manipulation macros for the drr_versioninfo field of the
64  * send stream header.
65  */
66 
67 /*
68  * Header types for zfs send streams.
69  */
70 typedef enum drr_headertype {
71 	DMU_SUBSTREAM = 0x1,
72 	DMU_COMPOUNDSTREAM = 0x2
73 } drr_headertype_t;
74 
75 #define	DMU_GET_STREAM_HDRTYPE(vi)	BF64_GET((vi), 0, 2)
76 #define	DMU_SET_STREAM_HDRTYPE(vi, x)	BF64_SET((vi), 0, 2, x)
77 
78 #define	DMU_GET_FEATUREFLAGS(vi)	BF64_GET((vi), 2, 30)
79 #define	DMU_SET_FEATUREFLAGS(vi, x)	BF64_SET((vi), 2, 30, x)
80 
81 /*
82  * Feature flags for zfs send streams (flags in drr_versioninfo)
83  */
84 
85 #define	DMU_BACKUP_FEATURE_DEDUP		(1 << 0)
86 #define	DMU_BACKUP_FEATURE_DEDUPPROPS		(1 << 1)
87 #define	DMU_BACKUP_FEATURE_SA_SPILL		(1 << 2)
88 /* flags #3 - #15 are reserved for incompatible closed-source implementations */
89 #define	DMU_BACKUP_FEATURE_EMBED_DATA		(1 << 16)
90 #define	DMU_BACKUP_FEATURE_LZ4			(1 << 17)
91 /* flag #18 is reserved for a Delphix feature */
92 #define	DMU_BACKUP_FEATURE_LARGE_BLOCKS		(1 << 19)
93 #define	DMU_BACKUP_FEATURE_RESUMING		(1 << 20)
94 /* flag #21 is reserved for the redacted send/receive feature */
95 #define	DMU_BACKUP_FEATURE_COMPRESSED		(1 << 22)
96 #define	DMU_BACKUP_FEATURE_LARGE_DNODE		(1 << 23)
97 #define	DMU_BACKUP_FEATURE_RAW			(1 << 24)
98 /* flag #25 is reserved for the ZSTD compression feature */
99 #define	DMU_BACKUP_FEATURE_HOLDS		(1 << 26)
100 
101 /*
102  * Mask of all supported backup features
103  */
104 #define	DMU_BACKUP_FEATURE_MASK	(DMU_BACKUP_FEATURE_DEDUP | \
105     DMU_BACKUP_FEATURE_DEDUPPROPS | DMU_BACKUP_FEATURE_SA_SPILL | \
106     DMU_BACKUP_FEATURE_EMBED_DATA | DMU_BACKUP_FEATURE_LZ4 | \
107     DMU_BACKUP_FEATURE_RESUMING | DMU_BACKUP_FEATURE_LARGE_BLOCKS | \
108     DMU_BACKUP_FEATURE_COMPRESSED | DMU_BACKUP_FEATURE_LARGE_DNODE | \
109     DMU_BACKUP_FEATURE_RAW | DMU_BACKUP_FEATURE_HOLDS)
110 
111 /* Are all features in the given flag word currently supported? */
112 #define	DMU_STREAM_SUPPORTED(x)	(!((x) & ~DMU_BACKUP_FEATURE_MASK))
113 
114 typedef enum dmu_send_resume_token_version {
115 	ZFS_SEND_RESUME_TOKEN_VERSION = 1
116 } dmu_send_resume_token_version_t;
117 
118 /*
119  * The drr_versioninfo field of the dmu_replay_record has the
120  * following layout:
121  *
122  *	64	56	48	40	32	24	16	8	0
123  *	+-------+-------+-------+-------+-------+-------+-------+-------+
124  *	|		reserved	|        feature-flags	    |C|S|
125  *	+-------+-------+-------+-------+-------+-------+-------+-------+
126  *
127  * The low order two bits indicate the header type: SUBSTREAM (0x1)
128  * or COMPOUNDSTREAM (0x2).  Using two bits for this is historical:
129  * this field used to be a version number, where the two version types
130  * were 1 and 2.  Using two bits for this allows earlier versions of
131  * the code to be able to recognize send streams that don't use any
132  * of the features indicated by feature flags.
133  */
134 
135 #define	DMU_BACKUP_MAGIC 0x2F5bacbacULL
136 
137 /*
138  * Send stream flags.  Bits 24-31 are reserved for vendor-specific
139  * implementations and should not be used.
140  */
141 #define	DRR_FLAG_CLONE		(1<<0)
142 #define	DRR_FLAG_CI_DATA	(1<<1)
143 /*
144  * This send stream, if it is a full send, includes the FREE and FREEOBJECT
145  * records that are created by the sending process.  This means that the send
146  * stream can be received as a clone, even though it is not an incremental.
147  * This is not implemented as a feature flag, because the receiving side does
148  * not need to have implemented it to receive this stream; it is fully backwards
149  * compatible.  We need a flag, though, because full send streams without it
150  * cannot necessarily be received as a clone correctly.
151  */
152 #define	DRR_FLAG_FREERECORDS	(1<<2)
153 /*
154  * When DRR_FLAG_SPILL_BLOCK is set it indicates the DRR_OBJECT_SPILL
155  * and DRR_SPILL_UNMODIFIED flags are meaningful in the send stream.
156  *
157  * When DRR_FLAG_SPILL_BLOCK is set, DRR_OBJECT records will have
158  * DRR_OBJECT_SPILL set if and only if they should have a spill block
159  * (either an existing one, or a new one in the send stream).  When clear
160  * the object does not have a spill block and any existing spill block
161  * should be freed.
162  *
163  * Similarly, when DRR_FLAG_SPILL_BLOCK is set, DRR_SPILL records will
164  * have DRR_SPILL_UNMODIFIED set if and only if they were included for
165  * backward compatibility purposes, and can be safely ignored by new versions
166  * of zfs receive.  Previous versions of ZFS which do not understand the
167  * DRR_FLAG_SPILL_BLOCK will process this record and recreate any missing
168  * spill blocks.
169  */
170 #define	DRR_FLAG_SPILL_BLOCK	(1<<3)
171 
172 /*
173  * flags in the drr_flags field in the DRR_WRITE, DRR_SPILL, DRR_OBJECT,
174  * DRR_WRITE_BYREF, and DRR_OBJECT_RANGE blocks
175  */
176 #define	DRR_CHECKSUM_DEDUP	(1<<0) /* not used for SPILL records */
177 #define	DRR_RAW_BYTESWAP	(1<<1)
178 #define	DRR_OBJECT_SPILL	(1<<2) /* OBJECT record has a spill block */
179 #define	DRR_SPILL_UNMODIFIED	(1<<2) /* SPILL record for unmodified block */
180 
181 #define	DRR_IS_DEDUP_CAPABLE(flags)	((flags) & DRR_CHECKSUM_DEDUP)
182 #define	DRR_IS_RAW_BYTESWAPPED(flags)	((flags) & DRR_RAW_BYTESWAP)
183 #define	DRR_OBJECT_HAS_SPILL(flags)	((flags) & DRR_OBJECT_SPILL)
184 #define	DRR_SPILL_IS_UNMODIFIED(flags)	((flags) & DRR_SPILL_UNMODIFIED)
185 
186 /* deal with compressed drr_write replay records */
187 #define	DRR_WRITE_COMPRESSED(drrw)	((drrw)->drr_compressiontype != 0)
188 #define	DRR_WRITE_PAYLOAD_SIZE(drrw) \
189 	(DRR_WRITE_COMPRESSED(drrw) ? (drrw)->drr_compressed_size : \
190 	(drrw)->drr_logical_size)
191 #define	DRR_SPILL_PAYLOAD_SIZE(drrs) \
192 	((drrs)->drr_compressed_size ? \
193 	(drrs)->drr_compressed_size : (drrs)->drr_length)
194 #define	DRR_OBJECT_PAYLOAD_SIZE(drro) \
195 	((drro)->drr_raw_bonuslen != 0 ? \
196 	(drro)->drr_raw_bonuslen : P2ROUNDUP((drro)->drr_bonuslen, 8))
197 
198 /*
199  * zfs ioctl command structure
200  */
201 typedef struct dmu_replay_record {
202 	enum {
203 		DRR_BEGIN, DRR_OBJECT, DRR_FREEOBJECTS,
204 		DRR_WRITE, DRR_FREE, DRR_END, DRR_WRITE_BYREF,
205 		DRR_SPILL, DRR_WRITE_EMBEDDED, DRR_OBJECT_RANGE,
206 		DRR_NUMTYPES
207 	} drr_type;
208 	uint32_t drr_payloadlen;
209 	union {
210 		struct drr_begin {
211 			uint64_t drr_magic;
212 			uint64_t drr_versioninfo; /* was drr_version */
213 			uint64_t drr_creation_time;
214 			dmu_objset_type_t drr_type;
215 			uint32_t drr_flags;
216 			uint64_t drr_toguid;
217 			uint64_t drr_fromguid;
218 			char drr_toname[MAXNAMELEN];
219 		} drr_begin;
220 		struct drr_end {
221 			zio_cksum_t drr_checksum;
222 			uint64_t drr_toguid;
223 		} drr_end;
224 		struct drr_object {
225 			uint64_t drr_object;
226 			dmu_object_type_t drr_type;
227 			dmu_object_type_t drr_bonustype;
228 			uint32_t drr_blksz;
229 			uint32_t drr_bonuslen;
230 			uint8_t drr_checksumtype;
231 			uint8_t drr_compress;
232 			uint8_t drr_dn_slots;
233 			uint8_t drr_flags;
234 			uint32_t drr_raw_bonuslen;
235 			uint64_t drr_toguid;
236 			/* only (possibly) nonzero for raw streams */
237 			uint8_t drr_indblkshift;
238 			uint8_t drr_nlevels;
239 			uint8_t drr_nblkptr;
240 			uint8_t drr_pad[5];
241 			uint64_t drr_maxblkid;
242 			/* bonus content follows */
243 		} drr_object;
244 		struct drr_freeobjects {
245 			uint64_t drr_firstobj;
246 			uint64_t drr_numobjs;
247 			uint64_t drr_toguid;
248 		} drr_freeobjects;
249 		struct drr_write {
250 			uint64_t drr_object;
251 			dmu_object_type_t drr_type;
252 			uint32_t drr_pad;
253 			uint64_t drr_offset;
254 			uint64_t drr_logical_size;
255 			uint64_t drr_toguid;
256 			uint8_t drr_checksumtype;
257 			uint8_t drr_flags;
258 			uint8_t drr_compressiontype;
259 			uint8_t drr_pad2[5];
260 			/* deduplication key */
261 			ddt_key_t drr_key;
262 			/* only nonzero if drr_compressiontype is not 0 */
263 			uint64_t drr_compressed_size;
264 			/* only nonzero for raw streams */
265 			uint8_t drr_salt[ZIO_DATA_SALT_LEN];
266 			uint8_t drr_iv[ZIO_DATA_IV_LEN];
267 			uint8_t drr_mac[ZIO_DATA_MAC_LEN];
268 			/* content follows */
269 		} drr_write;
270 		struct drr_free {
271 			uint64_t drr_object;
272 			uint64_t drr_offset;
273 			uint64_t drr_length;
274 			uint64_t drr_toguid;
275 		} drr_free;
276 		struct drr_write_byref {
277 			/* where to put the data */
278 			uint64_t drr_object;
279 			uint64_t drr_offset;
280 			uint64_t drr_length;
281 			uint64_t drr_toguid;
282 			/* where to find the prior copy of the data */
283 			uint64_t drr_refguid;
284 			uint64_t drr_refobject;
285 			uint64_t drr_refoffset;
286 			/* properties of the data */
287 			uint8_t drr_checksumtype;
288 			uint8_t drr_flags;
289 			uint8_t drr_pad2[6];
290 			ddt_key_t drr_key; /* deduplication key */
291 		} drr_write_byref;
292 		struct drr_spill {
293 			uint64_t drr_object;
294 			uint64_t drr_length;
295 			uint64_t drr_toguid;
296 			uint8_t drr_flags;
297 			uint8_t drr_compressiontype;
298 			uint8_t drr_pad[6];
299 			/* only nonzero for raw streams */
300 			uint64_t drr_compressed_size;
301 			uint8_t drr_salt[ZIO_DATA_SALT_LEN];
302 			uint8_t drr_iv[ZIO_DATA_IV_LEN];
303 			uint8_t drr_mac[ZIO_DATA_MAC_LEN];
304 			dmu_object_type_t drr_type;
305 			/* spill data follows */
306 		} drr_spill;
307 		struct drr_write_embedded {
308 			uint64_t drr_object;
309 			uint64_t drr_offset;
310 			/* logical length, should equal blocksize */
311 			uint64_t drr_length;
312 			uint64_t drr_toguid;
313 			uint8_t drr_compression;
314 			uint8_t drr_etype;
315 			uint8_t drr_pad[6];
316 			uint32_t drr_lsize; /* uncompressed size of payload */
317 			uint32_t drr_psize; /* compr. (real) size of payload */
318 			/* (possibly compressed) content follows */
319 		} drr_write_embedded;
320 		struct drr_object_range {
321 			uint64_t drr_firstobj;
322 			uint64_t drr_numslots;
323 			uint64_t drr_toguid;
324 			uint8_t drr_salt[ZIO_DATA_SALT_LEN];
325 			uint8_t drr_iv[ZIO_DATA_IV_LEN];
326 			uint8_t drr_mac[ZIO_DATA_MAC_LEN];
327 			uint8_t drr_flags;
328 			uint8_t drr_pad[3];
329 		} drr_object_range;
330 
331 		/*
332 		 * Nore: drr_checksum is overlaid with all record types
333 		 * except DRR_BEGIN.  Therefore its (non-pad) members
334 		 * must not overlap with members from the other structs.
335 		 * We accomplish this by putting its members at the very
336 		 * end of the struct.
337 		 */
338 		struct drr_checksum {
339 			uint64_t drr_pad[34];
340 			/*
341 			 * fletcher-4 checksum of everything preceding the
342 			 * checksum.
343 			 */
344 			zio_cksum_t drr_checksum;
345 		} drr_checksum;
346 	} drr_u;
347 } dmu_replay_record_t;
348 
349 /* diff record range types */
350 typedef enum diff_type {
351 	DDR_NONE = 0x1,
352 	DDR_INUSE = 0x2,
353 	DDR_FREE = 0x4
354 } diff_type_t;
355 
356 /*
357  * The diff reports back ranges of free or in-use objects.
358  */
359 typedef struct dmu_diff_record {
360 	uint64_t ddr_type;
361 	uint64_t ddr_first;
362 	uint64_t ddr_last;
363 } dmu_diff_record_t;
364 
365 typedef struct zinject_record {
366 	uint64_t	zi_objset;
367 	uint64_t	zi_object;
368 	uint64_t	zi_start;
369 	uint64_t	zi_end;
370 	uint64_t	zi_guid;
371 	uint32_t	zi_level;
372 	uint32_t	zi_error;
373 	uint64_t	zi_type;
374 	uint32_t	zi_freq;
375 	uint32_t	zi_failfast;
376 	char		zi_func[MAXNAMELEN];
377 	uint32_t	zi_iotype;
378 	int32_t		zi_duration;
379 	uint64_t	zi_timer;
380 	uint64_t	zi_nlanes;
381 	uint32_t	zi_cmd;
382 	uint32_t	zi_dvas;
383 } zinject_record_t;
384 
385 #define	ZINJECT_NULL		0x1
386 #define	ZINJECT_FLUSH_ARC	0x2
387 #define	ZINJECT_UNLOAD_SPA	0x4
388 
389 #define	ZI_NO_DVA		(-1)
390 
391 typedef enum zinject_type {
392 	ZINJECT_UNINITIALIZED,
393 	ZINJECT_DATA_FAULT,
394 	ZINJECT_DEVICE_FAULT,
395 	ZINJECT_LABEL_FAULT,
396 	ZINJECT_IGNORED_WRITES,
397 	ZINJECT_PANIC,
398 	ZINJECT_DELAY_IO,
399 	ZINJECT_DECRYPT_FAULT,
400 } zinject_type_t;
401 
402 typedef struct zfs_share {
403 	uint64_t	z_exportdata;
404 	uint64_t	z_sharedata;
405 	uint64_t	z_sharetype;	/* 0 = share, 1 = unshare */
406 	uint64_t	z_sharemax;  /* max length of share string */
407 } zfs_share_t;
408 
409 /*
410  * ZFS file systems may behave the usual, POSIX-compliant way, where
411  * name lookups are case-sensitive.  They may also be set up so that
412  * all the name lookups are case-insensitive, or so that only some
413  * lookups, the ones that set an FIGNORECASE flag, are case-insensitive.
414  */
415 typedef enum zfs_case {
416 	ZFS_CASE_SENSITIVE,
417 	ZFS_CASE_INSENSITIVE,
418 	ZFS_CASE_MIXED
419 } zfs_case_t;
420 
421 /*
422  * Note: this struct must have the same layout in 32-bit and 64-bit, so
423  * that 32-bit processes (like /sbin/zfs) can pass it to the 64-bit
424  * kernel.  Therefore, we add padding to it so that no "hidden" padding
425  * is automatically added on 64-bit (but not on 32-bit).
426  */
427 typedef struct zfs_cmd {
428 	char		zc_name[MAXPATHLEN];	/* name of pool or dataset */
429 	uint64_t	zc_nvlist_src;		/* really (char *) */
430 	uint64_t	zc_nvlist_src_size;
431 	uint64_t	zc_nvlist_dst;		/* really (char *) */
432 	uint64_t	zc_nvlist_dst_size;
433 	boolean_t	zc_nvlist_dst_filled;	/* put an nvlist in dst? */
434 	int		zc_pad2;
435 
436 	/*
437 	 * The following members are for legacy ioctls which haven't been
438 	 * converted to the new method.
439 	 */
440 	uint64_t	zc_history;		/* really (char *) */
441 	char		zc_value[MAXPATHLEN * 2];
442 	char		zc_string[MAXNAMELEN];
443 	uint64_t	zc_guid;
444 	uint64_t	zc_nvlist_conf;		/* really (char *) */
445 	uint64_t	zc_nvlist_conf_size;
446 	uint64_t	zc_cookie;
447 	uint64_t	zc_objset_type;
448 	uint64_t	zc_perm_action;
449 	uint64_t	zc_history_len;
450 	uint64_t	zc_history_offset;
451 	uint64_t	zc_obj;
452 	uint64_t	zc_iflags;		/* internal to zfs(7fs) */
453 	zfs_share_t	zc_share;
454 	dmu_objset_stats_t zc_objset_stats;
455 	dmu_replay_record_t zc_begin_record;
456 	zinject_record_t zc_inject_record;
457 	uint32_t	zc_defer_destroy;
458 	uint32_t	zc_flags;
459 	uint64_t	zc_action_handle;
460 	int		zc_cleanup_fd;
461 	uint8_t		zc_simple;
462 	uint8_t		zc_pad3[3];
463 	boolean_t	zc_resumable;
464 	uint32_t	zc_pad4;
465 	uint64_t	zc_sendobj;
466 	uint64_t	zc_fromobj;
467 	uint64_t	zc_createtxg;
468 	zfs_stat_t	zc_stat;
469 } zfs_cmd_t;
470 
471 typedef struct zfs_useracct {
472 	char zu_domain[256];
473 	uid_t zu_rid;
474 	uint32_t zu_pad;
475 	uint64_t zu_space;
476 } zfs_useracct_t;
477 
478 #define	ZFSDEV_MAX_MINOR	(1 << 16)
479 #define	ZFS_MIN_MINOR	(ZFSDEV_MAX_MINOR + 1)
480 
481 #define	ZPOOL_EXPORT_AFTER_SPLIT 0x1
482 
483 #ifdef _KERNEL
484 struct objset;
485 struct zfsvfs;
486 
487 typedef struct zfs_creat {
488 	nvlist_t	*zct_zplprops;
489 	nvlist_t	*zct_props;
490 } zfs_creat_t;
491 
492 extern dev_info_t *zfs_dip;
493 
494 extern int zfs_secpolicy_snapshot_perms(const char *, cred_t *);
495 extern int zfs_secpolicy_rename_perms(const char *, const char *, cred_t *);
496 extern int zfs_secpolicy_destroy_perms(const char *, cred_t *);
497 extern int zfs_busy(void);
498 extern void zfs_unmount_snap(const char *);
499 extern void zfs_destroy_unmount_origin(const char *);
500 extern int getzfsvfs_impl(struct objset *, struct zfsvfs **);
501 extern int getzfsvfs(const char *, struct zfsvfs **);
502 
503 /*
504  * ZFS minor numbers can refer to either a control device instance or
505  * a zvol. Depending on the value of zss_type, zss_data points to either
506  * a zvol_state_t or a zfs_onexit_t.
507  */
508 enum zfs_soft_state_type {
509 	ZSST_ZVOL,
510 	ZSST_CTLDEV
511 };
512 
513 typedef struct zfs_soft_state {
514 	enum zfs_soft_state_type zss_type;
515 	void *zss_data;
516 } zfs_soft_state_t;
517 
518 extern void *zfsdev_get_soft_state(minor_t minor,
519     enum zfs_soft_state_type which);
520 extern minor_t zfsdev_minor_alloc(void);
521 
522 extern void *zfsdev_state;
523 extern kmutex_t zfsdev_state_lock;
524 
525 #endif	/* _KERNEL */
526 
527 #ifdef	__cplusplus
528 }
529 #endif
530 
531 #endif	/* _SYS_ZFS_IOCTL_H */
532