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