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