xref: /freebsd/sys/contrib/openzfs/include/sys/dmu_objset.h (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
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) 2013 by Saso Kiselkov. All rights reserved.
26  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27  */
28 
29 /* Portions Copyright 2010 Robert Milkowski */
30 
31 #ifndef	_SYS_DMU_OBJSET_H
32 #define	_SYS_DMU_OBJSET_H
33 
34 #include <sys/spa.h>
35 #include <sys/arc.h>
36 #include <sys/txg.h>
37 #include <sys/zfs_context.h>
38 #include <sys/dnode.h>
39 #include <sys/zio.h>
40 #include <sys/zil.h>
41 #include <sys/sa.h>
42 #include <sys/zfs_ioctl.h>
43 
44 #ifdef	__cplusplus
45 extern "C" {
46 #endif
47 
48 extern krwlock_t os_lock;
49 
50 struct dsl_pool;
51 struct dsl_dataset;
52 struct dmu_tx;
53 
54 #define	OBJSET_PHYS_SIZE_V1	1024
55 #define	OBJSET_PHYS_SIZE_V2	2048
56 #define	OBJSET_PHYS_SIZE_V3	4096
57 
58 #define	OBJSET_BUF_HAS_USERUSED(buf) \
59 	(arc_buf_size(buf) >= OBJSET_PHYS_SIZE_V2)
60 #define	OBJSET_BUF_HAS_PROJECTUSED(buf) \
61 	(arc_buf_size(buf) >= OBJSET_PHYS_SIZE_V3)
62 
63 #define	OBJSET_FLAG_USERACCOUNTING_COMPLETE	(1ULL << 0)
64 #define	OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE	(1ULL << 1)
65 #define	OBJSET_FLAG_PROJECTQUOTA_COMPLETE	(1ULL << 2)
66 
67 /*
68  * This mask defines the set of flags which are "portable", meaning
69  * that they can be preserved when doing a raw encrypted zfs send.
70  * Flags included in this mask will be protected by os_portable_mac
71  * when the block of dnodes is encrypted. No portable flags currently
72  * exist.
73  */
74 #define	OBJSET_CRYPT_PORTABLE_FLAGS_MASK	(0)
75 
76 #if defined(__clang__)
77 #pragma clang diagnostic push
78 #pragma clang diagnostic ignored "-Wgnu-variable-sized-type-not-at-end"
79 #endif
80 typedef struct objset_phys {
81 	dnode_phys_t os_meta_dnode;
82 	zil_header_t os_zil_header;
83 	uint64_t os_type;
84 	uint64_t os_flags;
85 	uint8_t os_portable_mac[ZIO_OBJSET_MAC_LEN];
86 	uint8_t os_local_mac[ZIO_OBJSET_MAC_LEN];
87 	char os_pad0[OBJSET_PHYS_SIZE_V2 - sizeof (dnode_phys_t)*3 -
88 	    sizeof (zil_header_t) - sizeof (uint64_t)*2 -
89 	    2*ZIO_OBJSET_MAC_LEN];
90 	dnode_phys_t os_userused_dnode;
91 	dnode_phys_t os_groupused_dnode;
92 	dnode_phys_t os_projectused_dnode;
93 	char os_pad1[OBJSET_PHYS_SIZE_V3 - OBJSET_PHYS_SIZE_V2 -
94 	    sizeof (dnode_phys_t)];
95 } objset_phys_t;
96 #if defined(__clang__)
97 #pragma clang diagnostic pop
98 #endif
99 
100 typedef int (*dmu_objset_upgrade_cb_t)(objset_t *);
101 
102 #define	OBJSET_PROP_UNINITIALIZED	((uint64_t)-1)
103 struct objset {
104 	/* Immutable: */
105 	struct dsl_dataset *os_dsl_dataset;
106 	spa_t *os_spa;
107 	arc_buf_t *os_phys_buf;
108 	objset_phys_t *os_phys;
109 	boolean_t os_encrypted;
110 
111 	/*
112 	 * The following "special" dnodes have no parent, are exempt
113 	 * from dnode_move(), and are not recorded in os_dnodes, but they
114 	 * root their descendents in this objset using handles anyway, so
115 	 * that all access to dnodes from dbufs consistently uses handles.
116 	 */
117 	dnode_handle_t os_meta_dnode;
118 	dnode_handle_t os_userused_dnode;
119 	dnode_handle_t os_groupused_dnode;
120 	dnode_handle_t os_projectused_dnode;
121 	zilog_t *os_zil;
122 
123 	list_node_t os_evicting_node;
124 
125 	/* can change, under dsl_dir's locks: */
126 	uint64_t os_dnodesize; /* default dnode size for new objects */
127 	enum zio_checksum os_checksum;
128 	enum zio_compress os_compress;
129 	uint8_t os_complevel;
130 	uint8_t os_copies;
131 	enum zio_checksum os_dedup_checksum;
132 	boolean_t os_dedup_verify;
133 	zfs_logbias_op_t os_logbias;
134 	zfs_cache_type_t os_primary_cache;
135 	zfs_cache_type_t os_secondary_cache;
136 	zfs_prefetch_type_t os_prefetch;
137 	zfs_sync_type_t os_sync;
138 	zfs_direct_t os_direct;
139 	zfs_redundant_metadata_type_t os_redundant_metadata;
140 	uint64_t os_recordsize;
141 	/*
142 	 * The next four values are used as a cache of whatever's on disk, and
143 	 * are initialized the first time these properties are queried. Before
144 	 * being initialized with their real values, their values are
145 	 * OBJSET_PROP_UNINITIALIZED.
146 	 */
147 	uint64_t os_version;
148 	uint64_t os_normalization;
149 	uint64_t os_utf8only;
150 	uint64_t os_casesensitivity;
151 	/*
152 	 * The largest zpl file block allowed in special class.
153 	 * cached here instead of zfsvfs for easier access.
154 	 */
155 	int os_zpl_special_smallblock;
156 
157 	/*
158 	 * Pointer is constant; the blkptr it points to is protected by
159 	 * os_dsl_dataset->ds_bp_rwlock
160 	 */
161 	blkptr_t *os_rootbp;
162 
163 	/* no lock needed: */
164 	struct dmu_tx *os_synctx; /* XXX sketchy */
165 	zil_header_t os_zil_header;
166 	multilist_t os_synced_dnodes;
167 	uint64_t os_flags;
168 	uint64_t os_freed_dnodes;
169 	boolean_t os_rescan_dnodes;
170 	boolean_t os_raw_receive;
171 
172 	/* os_phys_buf should be written raw next txg */
173 	boolean_t os_next_write_raw[TXG_SIZE];
174 
175 	/* Protected by os_obj_lock */
176 	kmutex_t os_obj_lock;
177 	uint64_t os_obj_next_chunk;
178 
179 	/* Per-CPU next object to allocate, protected by atomic ops. */
180 	uint64_t *os_obj_next_percpu;
181 	int os_obj_next_percpu_len;
182 
183 	/* Protected by os_lock */
184 	kmutex_t os_lock;
185 	multilist_t os_dirty_dnodes[TXG_SIZE];
186 	list_t os_dnodes;
187 	list_t os_downgraded_dbufs;
188 
189 	/* Protects changes to DMU_{USER,GROUP,PROJECT}USED_OBJECT */
190 	kmutex_t os_userused_lock;
191 
192 	/* stuff we store for the user */
193 	kmutex_t os_user_ptr_lock;
194 	void *os_user_ptr;
195 	sa_os_t *os_sa;
196 
197 	/* kernel thread to upgrade this dataset */
198 	kmutex_t os_upgrade_lock;
199 	taskqid_t os_upgrade_id;
200 	dmu_objset_upgrade_cb_t os_upgrade_cb;
201 	boolean_t os_upgrade_exit;
202 	int os_upgrade_status;
203 };
204 
205 #define	DMU_META_OBJSET		0
206 #define	DMU_META_DNODE_OBJECT	0
207 #define	DMU_OBJECT_IS_SPECIAL(obj) ((int64_t)(obj) <= 0)
208 #define	DMU_META_DNODE(os)	((os)->os_meta_dnode.dnh_dnode)
209 #define	DMU_USERUSED_DNODE(os)	((os)->os_userused_dnode.dnh_dnode)
210 #define	DMU_GROUPUSED_DNODE(os)	((os)->os_groupused_dnode.dnh_dnode)
211 #define	DMU_PROJECTUSED_DNODE(os) ((os)->os_projectused_dnode.dnh_dnode)
212 
213 /* called from zpl */
214 int dmu_objset_hold(const char *name, const void *tag, objset_t **osp);
215 int dmu_objset_hold_flags(const char *name, boolean_t decrypt, const void *tag,
216     objset_t **osp);
217 int dmu_objset_own(const char *name, dmu_objset_type_t type,
218     boolean_t readonly, boolean_t decrypt, const void *tag, objset_t **osp);
219 int dmu_objset_own_obj(struct dsl_pool *dp, uint64_t obj,
220     dmu_objset_type_t type, boolean_t readonly, boolean_t decrypt,
221     const void *tag, objset_t **osp);
222 void dmu_objset_refresh_ownership(struct dsl_dataset *ds,
223     struct dsl_dataset **newds, boolean_t decrypt, const void *tag);
224 void dmu_objset_rele(objset_t *os, const void *tag);
225 void dmu_objset_rele_flags(objset_t *os, boolean_t decrypt, const void *tag);
226 void dmu_objset_disown(objset_t *os, boolean_t decrypt, const void *tag);
227 int dmu_objset_from_ds(struct dsl_dataset *ds, objset_t **osp);
228 
229 void dmu_objset_stats(objset_t *os, nvlist_t *nv);
230 void dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat);
231 void dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
232     uint64_t *usedobjsp, uint64_t *availobjsp);
233 uint64_t dmu_objset_fsid_guid(objset_t *os);
234 int dmu_objset_find_dp(struct dsl_pool *dp, uint64_t ddobj,
235     int func(struct dsl_pool *, struct dsl_dataset *, void *),
236     void *arg, int flags);
237 void dmu_objset_evict_dbufs(objset_t *os);
238 inode_timespec_t dmu_objset_snap_cmtime(objset_t *os);
239 
240 /* called from dsl */
241 void dmu_objset_sync(objset_t *os, zio_t *zio, dmu_tx_t *tx);
242 boolean_t dmu_objset_is_dirty(objset_t *os, uint64_t txg);
243 objset_t *dmu_objset_create_impl_dnstats(spa_t *spa, struct dsl_dataset *ds,
244     blkptr_t *bp, dmu_objset_type_t type, int levels, int blksz, int ibs,
245     dmu_tx_t *tx);
246 objset_t *dmu_objset_create_impl(spa_t *spa, struct dsl_dataset *ds,
247     blkptr_t *bp, dmu_objset_type_t type, dmu_tx_t *tx);
248 int dmu_objset_open_impl(spa_t *spa, struct dsl_dataset *ds, blkptr_t *bp,
249     objset_t **osp);
250 void dmu_objset_evict(objset_t *os);
251 void dmu_objset_sync_done(objset_t *os, dmu_tx_t *tx);
252 void dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx);
253 boolean_t dmu_objset_userused_enabled(objset_t *os);
254 void dmu_objset_userspace_upgrade(objset_t *os);
255 boolean_t dmu_objset_userspace_present(objset_t *os);
256 boolean_t dmu_objset_userobjused_enabled(objset_t *os);
257 boolean_t dmu_objset_userobjspace_upgradable(objset_t *os);
258 boolean_t dmu_objset_userobjspace_present(objset_t *os);
259 boolean_t dmu_objset_incompatible_encryption_version(objset_t *os);
260 boolean_t dmu_objset_projectquota_enabled(objset_t *os);
261 boolean_t dmu_objset_projectquota_present(objset_t *os);
262 boolean_t dmu_objset_projectquota_upgradable(objset_t *os);
263 void dmu_objset_id_quota_upgrade(objset_t *os);
264 int dmu_get_file_info(objset_t *os, dmu_object_type_t bonustype,
265     const void *data, zfs_file_info_t *zfi);
266 
267 int dmu_fsname(const char *snapname, char *buf);
268 
269 void dmu_objset_evict_done(objset_t *os);
270 void dmu_objset_willuse_space(objset_t *os, int64_t space, dmu_tx_t *tx);
271 
272 void dmu_objset_init(void);
273 void dmu_objset_fini(void);
274 
275 #ifdef	__cplusplus
276 }
277 #endif
278 
279 #endif /* _SYS_DMU_OBJSET_H */
280