xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/dsl_dataset.h (revision 2acef22db7808606888f8f92715629ff3ba555b9)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
223f9d6ad7SLin Ling  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
2334f2f8cfSMatthew Ahrens  * Copyright (c) 2013 by Delphix. All rights reserved.
244e3c9f44SBill Pijewski  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
25a7a845e4SSteven Hartland  * Copyright (c) 2013 Steven Hartland. All rights reserved.
26fa9e4066Sahrens  */
27fa9e4066Sahrens 
28fa9e4066Sahrens #ifndef	_SYS_DSL_DATASET_H
29fa9e4066Sahrens #define	_SYS_DSL_DATASET_H
30fa9e4066Sahrens 
31fa9e4066Sahrens #include <sys/dmu.h>
32fa9e4066Sahrens #include <sys/spa.h>
33fa9e4066Sahrens #include <sys/txg.h>
34c717a561Smaybee #include <sys/zio.h>
35fa9e4066Sahrens #include <sys/bplist.h>
361d452cf5Sahrens #include <sys/dsl_synctask.h>
37fa9e4066Sahrens #include <sys/zfs_context.h>
38cde58dbcSMatthew Ahrens #include <sys/dsl_deadlist.h>
393b2aab18SMatthew Ahrens #include <sys/refcount.h>
40fa9e4066Sahrens 
41fa9e4066Sahrens #ifdef	__cplusplus
42fa9e4066Sahrens extern "C" {
43fa9e4066Sahrens #endif
44fa9e4066Sahrens 
45fa9e4066Sahrens struct dsl_dataset;
46fa9e4066Sahrens struct dsl_dir;
47fa9e4066Sahrens struct dsl_pool;
48fa9e4066Sahrens 
4999653d4eSeschrock #define	DS_FLAG_INCONSISTENT	(1ULL<<0)
50745cd3c5Smaybee #define	DS_IS_INCONSISTENT(ds)	\
51745cd3c5Smaybee 	((ds)->ds_phys->ds_flags & DS_FLAG_INCONSISTENT)
52*2acef22dSMatthew Ahrens 
5399653d4eSeschrock /*
54*2acef22dSMatthew Ahrens  * Do not allow this dataset to be promoted.
5599653d4eSeschrock  */
5699653d4eSeschrock #define	DS_FLAG_NOPROMOTE	(1ULL<<1)
5799653d4eSeschrock 
58a9799022Sck /*
59a9799022Sck  * DS_FLAG_UNIQUE_ACCURATE is set if ds_unique_bytes has been correctly
60a9799022Sck  * calculated for head datasets (starting with SPA_VERSION_UNIQUE_ACCURATE,
61a9799022Sck  * refquota/refreservations).
62a9799022Sck  */
63a9799022Sck #define	DS_FLAG_UNIQUE_ACCURATE	(1ULL<<2)
64a9799022Sck 
65842727c2SChris Kirby /*
66842727c2SChris Kirby  * DS_FLAG_DEFER_DESTROY is set after 'zfs destroy -d' has been called
67842727c2SChris Kirby  * on a dataset. This allows the dataset to be destroyed using 'zfs release'.
68842727c2SChris Kirby  */
69842727c2SChris Kirby #define	DS_FLAG_DEFER_DESTROY	(1ULL<<3)
70842727c2SChris Kirby #define	DS_IS_DEFER_DESTROY(ds)	\
71842727c2SChris Kirby 	((ds)->ds_phys->ds_flags & DS_FLAG_DEFER_DESTROY)
72842727c2SChris Kirby 
73*2acef22dSMatthew Ahrens /*
74*2acef22dSMatthew Ahrens  * DS_FIELD_* are strings that are used in the "extensified" dataset zap object.
75*2acef22dSMatthew Ahrens  * They should be of the format <reverse-dns>:<field>.
76*2acef22dSMatthew Ahrens  */
77*2acef22dSMatthew Ahrens 
78ab04eb8eStimh /*
79ab04eb8eStimh  * DS_FLAG_CI_DATASET is set if the dataset contains a file system whose
80ab04eb8eStimh  * name lookups should be performed case-insensitively.
81ab04eb8eStimh  */
82ab04eb8eStimh #define	DS_FLAG_CI_DATASET	(1ULL<<16)
83ab04eb8eStimh 
843b2aab18SMatthew Ahrens #define	DS_CREATE_FLAG_NODIRTY	(1ULL<<24)
853b2aab18SMatthew Ahrens 
86fa9e4066Sahrens typedef struct dsl_dataset_phys {
87088f3894Sahrens 	uint64_t ds_dir_obj;		/* DMU_OT_DSL_DIR */
88088f3894Sahrens 	uint64_t ds_prev_snap_obj;	/* DMU_OT_DSL_DATASET */
89fa9e4066Sahrens 	uint64_t ds_prev_snap_txg;
90088f3894Sahrens 	uint64_t ds_next_snap_obj;	/* DMU_OT_DSL_DATASET */
91088f3894Sahrens 	uint64_t ds_snapnames_zapobj;	/* DMU_OT_DSL_DS_SNAP_MAP 0 for snaps */
92fa9e4066Sahrens 	uint64_t ds_num_children;	/* clone/snap children; ==0 for head */
93fa9e4066Sahrens 	uint64_t ds_creation_time;	/* seconds since 1970 */
94fa9e4066Sahrens 	uint64_t ds_creation_txg;
95cde58dbcSMatthew Ahrens 	uint64_t ds_deadlist_obj;	/* DMU_OT_DEADLIST */
96ad135b5dSChristopher Siden 	/*
97ad135b5dSChristopher Siden 	 * ds_referenced_bytes, ds_compressed_bytes, and ds_uncompressed_bytes
98ad135b5dSChristopher Siden 	 * include all blocks referenced by this dataset, including those
99ad135b5dSChristopher Siden 	 * shared with any other datasets.
100ad135b5dSChristopher Siden 	 */
101ad135b5dSChristopher Siden 	uint64_t ds_referenced_bytes;
102fa9e4066Sahrens 	uint64_t ds_compressed_bytes;
103fa9e4066Sahrens 	uint64_t ds_uncompressed_bytes;
10463360950Smp 	uint64_t ds_unique_bytes;	/* only relevant to snapshots */
105fa9e4066Sahrens 	/*
106fa9e4066Sahrens 	 * The ds_fsid_guid is a 56-bit ID that can change to avoid
107fa9e4066Sahrens 	 * collisions.  The ds_guid is a 64-bit ID that will never
108fa9e4066Sahrens 	 * change, so there is a small probability that it will collide.
109fa9e4066Sahrens 	 */
110fa9e4066Sahrens 	uint64_t ds_fsid_guid;
111fa9e4066Sahrens 	uint64_t ds_guid;
112088f3894Sahrens 	uint64_t ds_flags;		/* DS_FLAG_* */
113fa9e4066Sahrens 	blkptr_t ds_bp;
114bb0ade09Sahrens 	uint64_t ds_next_clones_obj;	/* DMU_OT_DSL_CLONES */
115bb0ade09Sahrens 	uint64_t ds_props_obj;		/* DMU_OT_DSL_PROPS for snaps */
116842727c2SChris Kirby 	uint64_t ds_userrefs_obj;	/* DMU_OT_USERREFS */
117842727c2SChris Kirby 	uint64_t ds_pad[5]; /* pad out to 320 bytes for good measure */
118fa9e4066Sahrens } dsl_dataset_phys_t;
119fa9e4066Sahrens 
120fa9e4066Sahrens typedef struct dsl_dataset {
121fa9e4066Sahrens 	/* Immutable: */
122fa9e4066Sahrens 	struct dsl_dir *ds_dir;
123fa9e4066Sahrens 	dsl_dataset_phys_t *ds_phys;
124fa9e4066Sahrens 	dmu_buf_t *ds_dbuf;
125fa9e4066Sahrens 	uint64_t ds_object;
12691ebeef5Sahrens 	uint64_t ds_fsid_guid;
127fa9e4066Sahrens 
12874e7dc98SMatthew Ahrens 	/* only used in syncing context, only valid for non-snapshots: */
12974e7dc98SMatthew Ahrens 	struct dsl_dataset *ds_prev;
130fa9e4066Sahrens 
131fa9e4066Sahrens 	/* has internal locking: */
132cde58dbcSMatthew Ahrens 	dsl_deadlist_t ds_deadlist;
133cde58dbcSMatthew Ahrens 	bplist_t ds_pending_deadlist;
134fa9e4066Sahrens 
135fa9e4066Sahrens 	/* protected by lock on pool's dp_dirty_datasets list */
136fa9e4066Sahrens 	txg_node_t ds_dirty_link;
137fa9e4066Sahrens 	list_node_t ds_synced_link;
138fa9e4066Sahrens 
139fa9e4066Sahrens 	/*
140fa9e4066Sahrens 	 * ds_phys->ds_<accounting> is also protected by ds_lock.
141fa9e4066Sahrens 	 * Protected by ds_lock:
142fa9e4066Sahrens 	 */
143fa9e4066Sahrens 	kmutex_t ds_lock;
144503ad85cSMatthew Ahrens 	objset_t *ds_objset;
145842727c2SChris Kirby 	uint64_t ds_userrefs;
1463b2aab18SMatthew Ahrens 	void *ds_owner;
147745cd3c5Smaybee 
148745cd3c5Smaybee 	/*
1493b2aab18SMatthew Ahrens 	 * Long holds prevent the ds from being destroyed; they allow the
1503b2aab18SMatthew Ahrens 	 * ds to remain held even after dropping the dp_config_rwlock.
1513b2aab18SMatthew Ahrens 	 * Owning counts as a long hold.  See the comments above
1523b2aab18SMatthew Ahrens 	 * dsl_pool_hold() for details.
153745cd3c5Smaybee 	 */
1543b2aab18SMatthew Ahrens 	refcount_t ds_longholds;
155fa9e4066Sahrens 
1561d452cf5Sahrens 	/* no locking; only for making guesses */
1571d452cf5Sahrens 	uint64_t ds_trysnap_txg;
1581d452cf5Sahrens 
15991ebeef5Sahrens 	/* for objset_open() */
16091ebeef5Sahrens 	kmutex_t ds_opening_lock;
16191ebeef5Sahrens 
162a9799022Sck 	uint64_t ds_reserved;	/* cached refreservation */
163a9799022Sck 	uint64_t ds_quota;	/* cached refquota */
164a9799022Sck 
1654e3c9f44SBill Pijewski 	kmutex_t ds_sendstream_lock;
1664e3c9f44SBill Pijewski 	list_t ds_sendstreams;
1674e3c9f44SBill Pijewski 
168fa9e4066Sahrens 	/* Protected by ds_lock; keep at end of struct for better locality */
169fa9e4066Sahrens 	char ds_snapname[MAXNAMELEN];
170fa9e4066Sahrens } dsl_dataset_t;
171fa9e4066Sahrens 
17299d5e173STim Haley /*
17399d5e173STim Haley  * The max length of a temporary tag prefix is the number of hex digits
17499d5e173STim Haley  * required to express UINT64_MAX plus one for the hyphen.
17599d5e173STim Haley  */
17699d5e173STim Haley #define	MAX_TAG_PREFIX_LEN	17
17799d5e173STim Haley 
178cde58dbcSMatthew Ahrens #define	dsl_dataset_is_snapshot(ds) \
179fa9e4066Sahrens 	((ds)->ds_phys->ds_num_children != 0)
180fa9e4066Sahrens 
181a9799022Sck #define	DS_UNIQUE_IS_ACCURATE(ds)	\
182a9799022Sck 	(((ds)->ds_phys->ds_flags & DS_FLAG_UNIQUE_ACCURATE) != 0)
183a9799022Sck 
1843b2aab18SMatthew Ahrens int dsl_dataset_hold(struct dsl_pool *dp, const char *name, void *tag,
1853b2aab18SMatthew Ahrens     dsl_dataset_t **dsp);
1863b2aab18SMatthew Ahrens int dsl_dataset_hold_obj(struct dsl_pool *dp, uint64_t dsobj, void *tag,
1873b2aab18SMatthew Ahrens     dsl_dataset_t **);
1883b2aab18SMatthew Ahrens void dsl_dataset_rele(dsl_dataset_t *ds, void *tag);
1893b2aab18SMatthew Ahrens int dsl_dataset_own(struct dsl_pool *dp, const char *name,
190503ad85cSMatthew Ahrens     void *tag, dsl_dataset_t **dsp);
191745cd3c5Smaybee int dsl_dataset_own_obj(struct dsl_pool *dp, uint64_t dsobj,
1923b2aab18SMatthew Ahrens     void *tag, dsl_dataset_t **dsp);
193503ad85cSMatthew Ahrens void dsl_dataset_disown(dsl_dataset_t *ds, void *tag);
1943b2aab18SMatthew Ahrens void dsl_dataset_name(dsl_dataset_t *ds, char *name);
1953b2aab18SMatthew Ahrens boolean_t dsl_dataset_tryown(dsl_dataset_t *ds, void *tag);
196745cd3c5Smaybee uint64_t dsl_dataset_create_sync(dsl_dir_t *pds, const char *lastname,
197745cd3c5Smaybee     dsl_dataset_t *origin, uint64_t flags, cred_t *, dmu_tx_t *);
198088f3894Sahrens uint64_t dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
199088f3894Sahrens     uint64_t flags, dmu_tx_t *tx);
2003b2aab18SMatthew Ahrens int dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors);
201681d9761SEric Taylor int dsl_dataset_promote(const char *name, char *conflsnap);
2023cb34c60Sahrens int dsl_dataset_clone_swap(dsl_dataset_t *clone, dsl_dataset_t *origin_head,
2033cb34c60Sahrens     boolean_t force);
2043b2aab18SMatthew Ahrens int dsl_dataset_rename_snapshot(const char *fsname,
2053b2aab18SMatthew Ahrens     const char *oldsnapname, const char *newsnapname, boolean_t recursive);
2063b2aab18SMatthew Ahrens int dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname,
2073b2aab18SMatthew Ahrens     minor_t cleanup_minor, const char *htag);
208fa9e4066Sahrens 
209c717a561Smaybee blkptr_t *dsl_dataset_get_blkptr(dsl_dataset_t *ds);
210fa9e4066Sahrens void dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx);
211fa9e4066Sahrens 
212fa9e4066Sahrens spa_t *dsl_dataset_get_spa(dsl_dataset_t *ds);
213fa9e4066Sahrens 
21434f2f8cfSMatthew Ahrens boolean_t dsl_dataset_modified_since_snap(dsl_dataset_t *ds,
21534f2f8cfSMatthew Ahrens     dsl_dataset_t *snap);
216f18faf3fSek 
217c717a561Smaybee void dsl_dataset_sync(dsl_dataset_t *os, zio_t *zio, dmu_tx_t *tx);
218fa9e4066Sahrens 
219b24ab676SJeff Bonwick void dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp,
220c717a561Smaybee     dmu_tx_t *tx);
221b24ab676SJeff Bonwick int dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp,
222b24ab676SJeff Bonwick     dmu_tx_t *tx, boolean_t async);
223c7cd2421SGeorge Wilson boolean_t dsl_dataset_block_freeable(dsl_dataset_t *ds, const blkptr_t *bp,
224c7cd2421SGeorge Wilson     uint64_t blk_birth);
225ea8dc4b6Seschrock uint64_t dsl_dataset_prev_snap_txg(dsl_dataset_t *ds);
226fa9e4066Sahrens 
227fa9e4066Sahrens void dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx);
228a2eea2e1Sahrens void dsl_dataset_stats(dsl_dataset_t *os, nvlist_t *nv);
229a2eea2e1Sahrens void dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat);
230a2eea2e1Sahrens void dsl_dataset_space(dsl_dataset_t *ds,
231a2eea2e1Sahrens     uint64_t *refdbytesp, uint64_t *availbytesp,
232a2eea2e1Sahrens     uint64_t *usedobjsp, uint64_t *availobjsp);
233a2eea2e1Sahrens uint64_t dsl_dataset_fsid_guid(dsl_dataset_t *ds);
23419b94df9SMatthew Ahrens int dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new,
23519b94df9SMatthew Ahrens     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp);
23619b94df9SMatthew Ahrens int dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap, dsl_dataset_t *last,
23719b94df9SMatthew Ahrens     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp);
2382e2c1355SMatthew Ahrens boolean_t dsl_dataset_is_dirty(dsl_dataset_t *ds);
239fa9e4066Sahrens 
240b1b8ab34Slling int dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf);
241b1b8ab34Slling 
242a9799022Sck int dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
2439082849eSck     uint64_t asize, uint64_t inflight, uint64_t *used,
2449082849eSck     uint64_t *ref_rsrv);
2453b2aab18SMatthew Ahrens int dsl_dataset_set_refquota(const char *dsname, zprop_source_t source,
24692241e0bSTom Erickson     uint64_t quota);
2473b2aab18SMatthew Ahrens int dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source,
24892241e0bSTom Erickson     uint64_t reservation);
249a9799022Sck 
2503b2aab18SMatthew Ahrens boolean_t dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier);
2513b2aab18SMatthew Ahrens void dsl_dataset_long_hold(dsl_dataset_t *ds, void *tag);
2523b2aab18SMatthew Ahrens void dsl_dataset_long_rele(dsl_dataset_t *ds, void *tag);
2533b2aab18SMatthew Ahrens boolean_t dsl_dataset_long_held(dsl_dataset_t *ds);
2543b2aab18SMatthew Ahrens 
2553b2aab18SMatthew Ahrens int dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone,
25691948b51SKeith M Wesolowski     dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx);
2573b2aab18SMatthew Ahrens void dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone,
2583b2aab18SMatthew Ahrens     dsl_dataset_t *origin_head, dmu_tx_t *tx);
2593b2aab18SMatthew Ahrens int dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname,
260ca48f36fSKeith M Wesolowski     dmu_tx_t *tx, boolean_t recv);
2613b2aab18SMatthew Ahrens void dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname,
2623b2aab18SMatthew Ahrens     dmu_tx_t *tx);
2633b2aab18SMatthew Ahrens 
2643b2aab18SMatthew Ahrens void dsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj,
2653b2aab18SMatthew Ahrens     dmu_tx_t *tx);
2663b2aab18SMatthew Ahrens void dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds);
2673b2aab18SMatthew Ahrens int dsl_dataset_get_snapname(dsl_dataset_t *ds);
2683b2aab18SMatthew Ahrens int dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name,
2693b2aab18SMatthew Ahrens     uint64_t *value);
2703b2aab18SMatthew Ahrens int dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx);
2713b2aab18SMatthew Ahrens void dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds,
2723b2aab18SMatthew Ahrens     zprop_source_t source, uint64_t value, dmu_tx_t *tx);
273a7027df1SMatthew Ahrens int dsl_dataset_rollback(const char *fsname, void *owner, nvlist_t *result);
274503ad85cSMatthew Ahrens 
275fa9e4066Sahrens #ifdef ZFS_DEBUG
276fa9e4066Sahrens #define	dprintf_ds(ds, fmt, ...) do { \
277fa9e4066Sahrens 	if (zfs_flags & ZFS_DEBUG_DPRINTF) { \
278fa9e4066Sahrens 	char *__ds_name = kmem_alloc(MAXNAMELEN, KM_SLEEP); \
279fa9e4066Sahrens 	dsl_dataset_name(ds, __ds_name); \
280fa9e4066Sahrens 	dprintf("ds=%s " fmt, __ds_name, __VA_ARGS__); \
281fa9e4066Sahrens 	kmem_free(__ds_name, MAXNAMELEN); \
282fa9e4066Sahrens 	} \
283fa9e4066Sahrens _NOTE(CONSTCOND) } while (0)
284fa9e4066Sahrens #else
285fa9e4066Sahrens #define	dprintf_ds(dd, fmt, ...)
286fa9e4066Sahrens #endif
287fa9e4066Sahrens 
288fa9e4066Sahrens #ifdef	__cplusplus
289fa9e4066Sahrens }
290fa9e4066Sahrens #endif
291fa9e4066Sahrens 
292fa9e4066Sahrens #endif /* _SYS_DSL_DATASET_H */
293