xref: /freebsd/sys/contrib/openzfs/include/sys/dsl_dir.h (revision 071ab5a1f3cbfd29c8fbec27f7e619418adaf074)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
25  * Copyright (c) 2014, Joyent, Inc. All rights reserved.
26  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27  */
28 
29 #ifndef	_SYS_DSL_DIR_H
30 #define	_SYS_DSL_DIR_H
31 
32 #include <sys/dmu.h>
33 #include <sys/dsl_deadlist.h>
34 #include <sys/dsl_pool.h>
35 #include <sys/dsl_synctask.h>
36 #include <sys/zfs_refcount.h>
37 #include <sys/zfs_context.h>
38 #include <sys/dsl_crypt.h>
39 #include <sys/bplist.h>
40 
41 #ifdef	__cplusplus
42 extern "C" {
43 #endif
44 
45 struct dsl_dataset;
46 struct zthr;
47 /*
48  * DD_FIELD_* are strings that are used in the "extensified" dsl_dir zap object.
49  * They should be of the format <reverse-dns>:<field>.
50  */
51 
52 #define	DD_FIELD_FILESYSTEM_COUNT	"com.joyent:filesystem_count"
53 #define	DD_FIELD_SNAPSHOT_COUNT		"com.joyent:snapshot_count"
54 #define	DD_FIELD_CRYPTO_KEY_OBJ		"com.datto:crypto_key_obj"
55 #define	DD_FIELD_LIVELIST		"com.delphix:livelist"
56 #define	DD_FIELD_SNAPSHOTS_CHANGED	"com.ixsystems:snapshots_changed"
57 
58 typedef enum dd_used {
59 	DD_USED_HEAD,
60 	DD_USED_SNAP,
61 	DD_USED_CHILD,
62 	DD_USED_CHILD_RSRV,
63 	DD_USED_REFRSRV,
64 	DD_USED_NUM
65 } dd_used_t;
66 
67 #define	DD_FLAG_USED_BREAKDOWN (1<<0)
68 
69 typedef struct dsl_dir_phys {
70 	uint64_t dd_creation_time; /* not actually used */
71 	uint64_t dd_head_dataset_obj;
72 	uint64_t dd_parent_obj;
73 	uint64_t dd_origin_obj;
74 	uint64_t dd_child_dir_zapobj;
75 	/*
76 	 * how much space our children are accounting for; for leaf
77 	 * datasets, == physical space used by fs + snaps
78 	 */
79 	uint64_t dd_used_bytes;
80 	uint64_t dd_compressed_bytes;
81 	uint64_t dd_uncompressed_bytes;
82 	/* Administrative quota setting */
83 	uint64_t dd_quota;
84 	/* Administrative reservation setting */
85 	uint64_t dd_reserved;
86 	uint64_t dd_props_zapobj;
87 	uint64_t dd_deleg_zapobj; /* dataset delegation permissions */
88 	uint64_t dd_flags;
89 	uint64_t dd_used_breakdown[DD_USED_NUM];
90 	uint64_t dd_clones; /* dsl_dir objects */
91 	uint64_t dd_pad[13]; /* pad out to 256 bytes for good measure */
92 } dsl_dir_phys_t;
93 
94 struct dsl_dir {
95 	dmu_buf_user_t dd_dbu;
96 
97 	/* These are immutable; no lock needed: */
98 	uint64_t dd_object;
99 	uint64_t dd_crypto_obj;
100 	dsl_pool_t *dd_pool;
101 
102 	/* Stable until user eviction; no lock needed: */
103 	dmu_buf_t *dd_dbuf;
104 
105 	/* protected by lock on pool's dp_dirty_dirs list */
106 	txg_node_t dd_dirty_link;
107 
108 	/* protected by dp_config_rwlock */
109 	dsl_dir_t *dd_parent;
110 
111 	/* Protected by dd_lock */
112 	kmutex_t dd_lock;
113 	list_t dd_props; /* list of dsl_prop_record_t's */
114 	inode_timespec_t dd_snap_cmtime; /* last snapshot namespace change */
115 	uint64_t dd_origin_txg;
116 
117 	/* gross estimate of space used by in-flight tx's */
118 	uint64_t dd_tempreserved[TXG_SIZE];
119 	/* amount of space we expect to write; == amount of dirty data */
120 	uint64_t dd_space_towrite[TXG_SIZE];
121 
122 	dsl_deadlist_t dd_livelist;
123 	bplist_t dd_pending_frees;
124 	bplist_t dd_pending_allocs;
125 
126 	kmutex_t dd_activity_lock;
127 	kcondvar_t dd_activity_cv;
128 	boolean_t dd_activity_cancelled;
129 	uint64_t dd_activity_waiters;
130 
131 	/* protected by dd_lock; keep at end of struct for better locality */
132 	char dd_myname[ZFS_MAX_DATASET_NAME_LEN];
133 };
134 
135 static inline dsl_dir_phys_t *
dsl_dir_phys(dsl_dir_t * dd)136 dsl_dir_phys(dsl_dir_t *dd)
137 {
138 	return (dd->dd_dbuf->db_data);
139 }
140 
141 void dsl_dir_rele(dsl_dir_t *dd, const void *tag);
142 void dsl_dir_async_rele(dsl_dir_t *dd, const void *tag);
143 int dsl_dir_hold(dsl_pool_t *dp, const char *name, const void *tag,
144     dsl_dir_t **, const char **tail);
145 int dsl_dir_hold_obj(dsl_pool_t *dp, uint64_t ddobj,
146     const char *tail, const void *tag, dsl_dir_t **);
147 void dsl_dir_name(dsl_dir_t *dd, char *buf);
148 int dsl_dir_namelen(dsl_dir_t *dd);
149 uint64_t dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds,
150     const char *name, dmu_tx_t *tx);
151 
152 uint64_t dsl_dir_get_used(dsl_dir_t *dd);
153 uint64_t dsl_dir_get_compressed(dsl_dir_t *dd);
154 uint64_t dsl_dir_get_quota(dsl_dir_t *dd);
155 uint64_t dsl_dir_get_reservation(dsl_dir_t *dd);
156 uint64_t dsl_dir_get_compressratio(dsl_dir_t *dd);
157 uint64_t dsl_dir_get_logicalused(dsl_dir_t *dd);
158 uint64_t dsl_dir_get_usedsnap(dsl_dir_t *dd);
159 uint64_t dsl_dir_get_usedds(dsl_dir_t *dd);
160 uint64_t dsl_dir_get_usedrefreserv(dsl_dir_t *dd);
161 uint64_t dsl_dir_get_usedchild(dsl_dir_t *dd);
162 void dsl_dir_get_origin(dsl_dir_t *dd, char *buf);
163 int dsl_dir_get_filesystem_count(dsl_dir_t *dd, uint64_t *count);
164 int dsl_dir_get_snapshot_count(dsl_dir_t *dd, uint64_t *count);
165 
166 void dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv);
167 uint64_t dsl_dir_space_available(dsl_dir_t *dd,
168     dsl_dir_t *ancestor, int64_t delta, int ondiskonly);
169 void dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx);
170 void dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx);
171 int dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t mem,
172     uint64_t asize, boolean_t netfree, void **tr_cookiep, dmu_tx_t *tx);
173 void dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx);
174 void dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx);
175 void dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type,
176     int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx);
177 void dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta,
178     dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx);
179 void dsl_dir_diduse_transfer_space(dsl_dir_t *dd, int64_t used,
180     int64_t compressed, int64_t uncompressed, int64_t tonew,
181     dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx);
182 int dsl_dir_set_quota(const char *ddname, zprop_source_t source,
183     uint64_t quota);
184 int dsl_dir_set_reservation(const char *ddname, zprop_source_t source,
185     uint64_t reservation);
186 int dsl_dir_activate_fs_ss_limit(const char *);
187 int dsl_fs_ss_limit_check(dsl_dir_t *, uint64_t, zfs_prop_t, dsl_dir_t *,
188     cred_t *);
189 void dsl_fs_ss_count_adjust(dsl_dir_t *, int64_t, const char *, dmu_tx_t *);
190 int dsl_dir_rename(const char *oldname, const char *newname);
191 int dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd,
192     uint64_t fs_cnt, uint64_t ss_cnt, uint64_t space, cred_t *);
193 boolean_t dsl_dir_is_clone(dsl_dir_t *dd);
194 void dsl_dir_new_refreservation(dsl_dir_t *dd, struct dsl_dataset *ds,
195     uint64_t reservation, cred_t *cr, dmu_tx_t *tx);
196 void dsl_dir_snap_cmtime_update(dsl_dir_t *dd, dmu_tx_t *tx);
197 inode_timespec_t dsl_dir_snap_cmtime(dsl_dir_t *dd);
198 void dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd, uint64_t value,
199     dmu_tx_t *tx);
200 void dsl_dir_zapify(dsl_dir_t *dd, dmu_tx_t *tx);
201 boolean_t dsl_dir_is_zapified(dsl_dir_t *dd);
202 int dsl_dir_livelist_open(dsl_dir_t *dd, uint64_t obj);
203 void dsl_dir_livelist_close(dsl_dir_t *dd);
204 void dsl_dir_remove_livelist(dsl_dir_t *dd, dmu_tx_t *tx, boolean_t total);
205 int dsl_dir_wait(dsl_dir_t *dd, dsl_dataset_t *ds, zfs_wait_activity_t activity,
206     boolean_t *waited);
207 void dsl_dir_cancel_waiters(dsl_dir_t *dd);
208 
209 /* internal reserved dir name */
210 #define	MOS_DIR_NAME "$MOS"
211 #define	ORIGIN_DIR_NAME "$ORIGIN"
212 #define	FREE_DIR_NAME "$FREE"
213 #define	LEAK_DIR_NAME "$LEAK"
214 
215 #ifdef ZFS_DEBUG
216 #define	dprintf_dd(dd, fmt, ...) do { \
217 	if (zfs_flags & ZFS_DEBUG_DPRINTF) { \
218 	char *__ds_name = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP); \
219 	dsl_dir_name(dd, __ds_name); \
220 	dprintf("dd=%s " fmt, __ds_name, __VA_ARGS__); \
221 	kmem_free(__ds_name, ZFS_MAX_DATASET_NAME_LEN); \
222 	} \
223 } while (0)
224 #else
225 #define	dprintf_dd(dd, fmt, ...)
226 #endif
227 
228 #ifdef	__cplusplus
229 }
230 #endif
231 
232 #endif /* _SYS_DSL_DIR_H */
233