1fa9e406ahrens/*
2fa9e406ahrens * CDDL HEADER START
3fa9e406ahrens *
4fa9e406ahrens * The contents of this file are subject to the terms of the
5ea8dc4beschrock * Common Development and Distribution License (the "License").
6ea8dc4beschrock * You may not use this file except in compliance with the License.
7fa9e406ahrens *
8fa9e406ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e406ahrens * or http://www.opensolaris.org/os/licensing.
10fa9e406ahrens * See the License for the specific language governing permissions
11fa9e406ahrens * and limitations under the License.
12fa9e406ahrens *
13fa9e406ahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e406ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e406ahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e406ahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e406ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e406ahrens *
19fa9e406ahrens * CDDL HEADER END
20fa9e406ahrens */
21fa9e406ahrens/*
223f9d6adLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
238671400Serapheim Dimitropoulos * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
24a2afb61Jerry Jelinek * Copyright (c) 2014, Joyent, Inc. All rights reserved.
25bc9014eJustin Gibbs * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
26fa9e406ahrens */
27fa9e406ahrens
28fa9e406ahrens#ifndef	_SYS_DSL_DIR_H
29fa9e406ahrens#define	_SYS_DSL_DIR_H
30fa9e406ahrens
31fa9e406ahrens#include <sys/dmu.h>
32fa9e406ahrens#include <sys/dsl_pool.h>
331d452cfahrens#include <sys/dsl_synctask.h>
34fa9e406ahrens#include <sys/refcount.h>
35fa9e406ahrens#include <sys/zfs_context.h>
36eb63303Tom Caputi#include <sys/dsl_crypt.h>
37fa9e406ahrens
38fa9e406ahrens#ifdef	__cplusplus
39fa9e406ahrensextern "C" {
40fa9e406ahrens#endif
41fa9e406ahrens
42fa9e406ahrensstruct dsl_dataset;
43fa9e406ahrens
44a2afb61Jerry Jelinek/*
45a2afb61Jerry Jelinek * DD_FIELD_* are strings that are used in the "extensified" dsl_dir zap object.
46a2afb61Jerry Jelinek * They should be of the format <reverse-dns>:<field>.
47a2afb61Jerry Jelinek */
48a2afb61Jerry Jelinek
49a2afb61Jerry Jelinek#define	DD_FIELD_FILESYSTEM_COUNT	"com.joyent:filesystem_count"
50a2afb61Jerry Jelinek#define	DD_FIELD_SNAPSHOT_COUNT		"com.joyent:snapshot_count"
515cabbc6Prashanth Sreenivasa#define	DD_FIELD_LAST_REMAP_TXG		"com.delphix:last_remap_txg"
52eb63303Tom Caputi#define	DD_FIELD_CRYPTO_KEY_OBJ		"com.datto:crypto_key_obj"
53a2afb61Jerry Jelinek
5474e7dc9Matthew Ahrenstypedef enum dd_used {
5574e7dc9Matthew Ahrens	DD_USED_HEAD,
5674e7dc9Matthew Ahrens	DD_USED_SNAP,
5774e7dc9Matthew Ahrens	DD_USED_CHILD,
5874e7dc9Matthew Ahrens	DD_USED_CHILD_RSRV,
5974e7dc9Matthew Ahrens	DD_USED_REFRSRV,
6074e7dc9Matthew Ahrens	DD_USED_NUM
6174e7dc9Matthew Ahrens} dd_used_t;
6274e7dc9Matthew Ahrens
6374e7dc9Matthew Ahrens#define	DD_FLAG_USED_BREAKDOWN (1<<0)
6474e7dc9Matthew Ahrens
65fa9e406ahrenstypedef struct dsl_dir_phys {
66a2eea2eahrens	uint64_t dd_creation_time; /* not actually used */
67fa9e406ahrens	uint64_t dd_head_dataset_obj;
68fa9e406ahrens	uint64_t dd_parent_obj;
693cb34c6ahrens	uint64_t dd_origin_obj;
70fa9e406ahrens	uint64_t dd_child_dir_zapobj;
71fa9e406ahrens	/*
72fa9e406ahrens	 * how much space our children are accounting for; for leaf
73fa9e406ahrens	 * datasets, == physical space used by fs + snaps
74fa9e406ahrens	 */
75fa9e406ahrens	uint64_t dd_used_bytes;
76fa9e406ahrens	uint64_t dd_compressed_bytes;
77fa9e406ahrens	uint64_t dd_uncompressed_bytes;
78fa9e406ahrens	/* Administrative quota setting */
79fa9e406ahrens	uint64_t dd_quota;
80fa9e406ahrens	/* Administrative reservation setting */
81fa9e406ahrens	uint64_t dd_reserved;
82fa9e406ahrens	uint64_t dd_props_zapobj;
83ecd6cf8marks	uint64_t dd_deleg_zapobj; /* dataset delegation permissions */
8474e7dc9Matthew Ahrens	uint64_t dd_flags;
8574e7dc9Matthew Ahrens	uint64_t dd_used_breakdown[DD_USED_NUM];
86cde58dbMatthew Ahrens	uint64_t dd_clones; /* dsl_dir objects */
87cde58dbMatthew Ahrens	uint64_t dd_pad[13]; /* pad out to 256 bytes for good measure */
88fa9e406ahrens} dsl_dir_phys_t;
89fa9e406ahrens
90fa9e406ahrensstruct dsl_dir {
91bc9014eJustin Gibbs	dmu_buf_user_t dd_dbu;
92bc9014eJustin Gibbs
93fa9e406ahrens	/* These are immutable; no lock needed: */
94fa9e406ahrens	uint64_t dd_object;
95eb63303Tom Caputi	uint64_t dd_crypto_obj;
96fa9e406ahrens	dsl_pool_t *dd_pool;
97fa9e406ahrens
98c137962Justin T. Gibbs	/* Stable until user eviction; no lock needed: */
99c137962Justin T. Gibbs	dmu_buf_t *dd_dbuf;
100c137962Justin T. Gibbs
101fa9e406ahrens	/* protected by lock on pool's dp_dirty_dirs list */
102fa9e406ahrens	txg_node_t dd_dirty_link;
103fa9e406ahrens
104fa9e406ahrens	/* protected by dp_config_rwlock */
105fa9e406ahrens	dsl_dir_t *dd_parent;
106fa9e406ahrens
107fa9e406ahrens	/* Protected by dd_lock */
108fa9e406ahrens	kmutex_t dd_lock;
10903bad06Justin Gibbs	list_t dd_props; /* list of dsl_prop_record_t's */
11071eb053Chris Kirby	timestruc_t dd_snap_cmtime; /* last time snapshot namespace changed */
1113f9d6adLin Ling	uint64_t dd_origin_txg;
112fa9e406ahrens
113fa9e406ahrens	/* gross estimate of space used by in-flight tx's */
114fa9e406ahrens	uint64_t dd_tempreserved[TXG_SIZE];
115fa9e406ahrens	/* amount of space we expect to write; == amount of dirty data */
116fa9e406ahrens	int64_t dd_space_towrite[TXG_SIZE];
117fa9e406ahrens
118fa9e406ahrens	/* protected by dd_lock; keep at end of struct for better locality */
1199adfa60Matthew Ahrens	char dd_myname[ZFS_MAX_DATASET_NAME_LEN];
120fa9e406ahrens};
121fa9e406ahrens
122c137962Justin T. Gibbsinline dsl_dir_phys_t *
123c137962Justin T. Gibbsdsl_dir_phys(dsl_dir_t *dd)
124c137962Justin T. Gibbs{
125c137962Justin T. Gibbs	return (dd->dd_dbuf->db_data);
126c137962Justin T. Gibbs}
127c137962Justin T. Gibbs
1283b2aab1Matthew Ahrensvoid dsl_dir_rele(dsl_dir_t *dd, void *tag);
129bc9014eJustin Gibbsvoid dsl_dir_async_rele(dsl_dir_t *dd, void *tag);
1303b2aab1Matthew Ahrensint dsl_dir_hold(dsl_pool_t *dp, const char *name, void *tag,
1313b2aab1Matthew Ahrens    dsl_dir_t **, const char **tail);
1323b2aab1Matthew Ahrensint dsl_dir_hold_obj(dsl_pool_t *dp, uint64_t ddobj,
133ea8dc4beschrock    const char *tail, void *tag, dsl_dir_t **);
134fa9e406ahrensvoid dsl_dir_name(dsl_dir_t *dd, char *buf);
135b7661ccmmusanteint dsl_dir_namelen(dsl_dir_t *dd);
136088f389ahrensuint64_t dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds,
137088f389ahrens    const char *name, dmu_tx_t *tx);
138dfc1153Chris Williamson
139dfc1153Chris Williamsonuint64_t dsl_dir_get_used(dsl_dir_t *dd);
1408671400Serapheim Dimitropoulosuint64_t dsl_dir_get_compressed(dsl_dir_t *dd);
141dfc1153Chris Williamsonuint64_t dsl_dir_get_quota(dsl_dir_t *dd);
142dfc1153Chris Williamsonuint64_t dsl_dir_get_reservation(dsl_dir_t *dd);
143dfc1153Chris Williamsonuint64_t dsl_dir_get_compressratio(dsl_dir_t *dd);
144dfc1153Chris Williamsonuint64_t dsl_dir_get_logicalused(dsl_dir_t *dd);
145dfc1153Chris Williamsonuint64_t dsl_dir_get_usedsnap(dsl_dir_t *dd);
146dfc1153Chris Williamsonuint64_t dsl_dir_get_usedds(dsl_dir_t *dd);
147dfc1153Chris Williamsonuint64_t dsl_dir_get_usedrefreserv(dsl_dir_t *dd);
148dfc1153Chris Williamsonuint64_t dsl_dir_get_usedchild(dsl_dir_t *dd);
149dfc1153Chris Williamsonvoid dsl_dir_get_origin(dsl_dir_t *dd, char *buf);
150dfc1153Chris Williamsonint dsl_dir_get_filesystem_count(dsl_dir_t *dd, uint64_t *count);
151dfc1153Chris Williamsonint dsl_dir_get_snapshot_count(dsl_dir_t *dd, uint64_t *count);
1525cabbc6Prashanth Sreenivasaint dsl_dir_get_remaptxg(dsl_dir_t *dd, uint64_t *count);
153dfc1153Chris Williamson
154a2eea2eahrensvoid dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv);
155a2eea2eahrensuint64_t dsl_dir_space_available(dsl_dir_t *dd,
156a2eea2eahrens    dsl_dir_t *ancestor, int64_t delta, int ondiskonly);
157fa9e406ahrensvoid dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx);
158fa9e406ahrensvoid dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx);
159fa9e406ahrensint dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t mem,
16061e255cMatthew Ahrens    uint64_t asize, boolean_t netfree, void **tr_cookiep, dmu_tx_t *tx);
161fa9e406ahrensvoid dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx);
162fa9e406ahrensvoid dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx);
16374e7dc9Matthew Ahrensvoid dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type,
164fa9e406ahrens    int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx);
16574e7dc9Matthew Ahrensvoid dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta,
16674e7dc9Matthew Ahrens    dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx);
16792241e0Tom Ericksonint dsl_dir_set_quota(const char *ddname, zprop_source_t source,
16892241e0Tom Erickson    uint64_t quota);
16992241e0Tom Ericksonint dsl_dir_set_reservation(const char *ddname, zprop_source_t source,
17092241e0Tom Erickson    uint64_t reservation);
171a2afb61Jerry Jelinekint dsl_dir_activate_fs_ss_limit(const char *);
172a2afb61Jerry Jelinekint dsl_fs_ss_limit_check(dsl_dir_t *, uint64_t, zfs_prop_t, dsl_dir_t *,
173a2afb61Jerry Jelinek    cred_t *);
174a2afb61Jerry Jelinekvoid dsl_fs_ss_count_adjust(dsl_dir_t *, int64_t, const char *, dmu_tx_t *);
1755cabbc6Prashanth Sreenivasaint dsl_dir_update_last_remap_txg(dsl_dir_t *, uint64_t);
1763b2aab1Matthew Ahrensint dsl_dir_rename(const char *oldname, const char *newname);
177a2afb61Jerry Jelinekint dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd,
178a2afb61Jerry Jelinek    uint64_t fs_cnt, uint64_t ss_cnt, uint64_t space, cred_t *);
179088f389ahrensboolean_t dsl_dir_is_clone(dsl_dir_t *dd);
1800b383afChris Kirbyvoid dsl_dir_new_refreservation(dsl_dir_t *dd, struct dsl_dataset *ds,
1810b383afChris Kirby    uint64_t reservation, cred_t *cr, dmu_tx_t *tx);
18271eb053Chris Kirbyvoid dsl_dir_snap_cmtime_update(dsl_dir_t *dd);
18371eb053Chris Kirbytimestruc_t dsl_dir_snap_cmtime(dsl_dir_t *dd);
1843b2aab1Matthew Ahrensvoid dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd, uint64_t value,
1853b2aab1Matthew Ahrens    dmu_tx_t *tx);
186a2afb61Jerry Jelinekvoid dsl_dir_zapify(dsl_dir_t *dd, dmu_tx_t *tx);
187a2afb61Jerry Jelinekboolean_t dsl_dir_is_zapified(dsl_dir_t *dd);
188fa9e406ahrens
18915f66a7ek/* internal reserved dir name */
19015f66a7ek#define	MOS_DIR_NAME "$MOS"
191088f389ahrens#define	ORIGIN_DIR_NAME "$ORIGIN"
192cde58dbMatthew Ahrens#define	FREE_DIR_NAME "$FREE"
1937fd05acMatthew Ahrens#define	LEAK_DIR_NAME "$LEAK"
19415f66a7ek
195fa9e406ahrens#ifdef ZFS_DEBUG
196fa9e406ahrens#define	dprintf_dd(dd, fmt, ...) do { \
197fa9e406ahrens	if (zfs_flags & ZFS_DEBUG_DPRINTF) { \
1989adfa60Matthew Ahrens	char *__ds_name = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP); \
199fa9e406ahrens	dsl_dir_name(dd, __ds_name); \
200fa9e406ahrens	dprintf("dd=%s " fmt, __ds_name, __VA_ARGS__); \
2019adfa60Matthew Ahrens	kmem_free(__ds_name, ZFS_MAX_DATASET_NAME_LEN); \
202fa9e406ahrens	} \
203fa9e406ahrens_NOTE(CONSTCOND) } while (0)
204fa9e406ahrens#else
205fa9e406ahrens#define	dprintf_dd(dd, fmt, ...)
206fa9e406ahrens#endif
207fa9e406ahrens
208fa9e406ahrens#ifdef	__cplusplus
209fa9e406ahrens}
210fa9e406ahrens#endif
211fa9e406ahrens
212fa9e406ahrens#endif /* _SYS_DSL_DIR_H */
213