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