]>
Commit | Line | Data |
---|---|---|
34dc7c2f BB |
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 http://www.opensolaris.org/os/licensing. | |
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 | /* | |
428870ff | 22 | * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. |
bc77ba73 | 23 | * Copyright (c) 2012, 2016 by Delphix. All rights reserved. |
3a17a7a9 | 24 | * Copyright (c) 2013 by Saso Kiselkov. All rights reserved. |
788eb90c | 25 | * Copyright (c) 2013, Joyent, Inc. All rights reserved. |
0c66c32d | 26 | * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. |
1cddb8c9 | 27 | * Copyright (c) 2015 Nexenta Systems, Inc. All rights reserved. |
9c43027b | 28 | * Copyright (c) 2015, STRATO AG, Inc. All rights reserved. |
a0bd735a | 29 | * Copyright (c) 2016 Actifio, Inc. All rights reserved. |
34dc7c2f BB |
30 | */ |
31 | ||
428870ff BB |
32 | /* Portions Copyright 2010 Robert Milkowski */ |
33 | ||
34dc7c2f BB |
34 | #include <sys/cred.h> |
35 | #include <sys/zfs_context.h> | |
36 | #include <sys/dmu_objset.h> | |
37 | #include <sys/dsl_dir.h> | |
38 | #include <sys/dsl_dataset.h> | |
39 | #include <sys/dsl_prop.h> | |
40 | #include <sys/dsl_pool.h> | |
41 | #include <sys/dsl_synctask.h> | |
42 | #include <sys/dsl_deleg.h> | |
43 | #include <sys/dnode.h> | |
44 | #include <sys/dbuf.h> | |
45 | #include <sys/zvol.h> | |
46 | #include <sys/dmu_tx.h> | |
34dc7c2f BB |
47 | #include <sys/zap.h> |
48 | #include <sys/zil.h> | |
49 | #include <sys/dmu_impl.h> | |
50 | #include <sys/zfs_ioctl.h> | |
428870ff | 51 | #include <sys/sa.h> |
572e2857 | 52 | #include <sys/zfs_onexit.h> |
13fe0198 | 53 | #include <sys/dsl_destroy.h> |
9c43027b | 54 | #include <sys/vdev.h> |
f74b821a | 55 | #include <sys/policy.h> |
572e2857 BB |
56 | |
57 | /* | |
58 | * Needed to close a window in dnode_move() that allows the objset to be freed | |
59 | * before it can be safely accessed. | |
60 | */ | |
61 | krwlock_t os_lock; | |
62 | ||
9c43027b AJ |
63 | /* |
64 | * Tunable to overwrite the maximum number of threads for the parallization | |
65 | * of dmu_objset_find_dp, needed to speed up the import of pools with many | |
66 | * datasets. | |
67 | * Default is 4 times the number of leaf vdevs. | |
68 | */ | |
69 | int dmu_find_threads = 0; | |
70 | ||
71 | static void dmu_objset_find_dp_cb(void *arg); | |
72 | ||
572e2857 BB |
73 | void |
74 | dmu_objset_init(void) | |
75 | { | |
76 | rw_init(&os_lock, NULL, RW_DEFAULT, NULL); | |
77 | } | |
78 | ||
79 | void | |
80 | dmu_objset_fini(void) | |
81 | { | |
82 | rw_destroy(&os_lock); | |
83 | } | |
34dc7c2f BB |
84 | |
85 | spa_t * | |
86 | dmu_objset_spa(objset_t *os) | |
87 | { | |
428870ff | 88 | return (os->os_spa); |
34dc7c2f BB |
89 | } |
90 | ||
91 | zilog_t * | |
92 | dmu_objset_zil(objset_t *os) | |
93 | { | |
428870ff | 94 | return (os->os_zil); |
34dc7c2f BB |
95 | } |
96 | ||
97 | dsl_pool_t * | |
98 | dmu_objset_pool(objset_t *os) | |
99 | { | |
100 | dsl_dataset_t *ds; | |
101 | ||
428870ff | 102 | if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir) |
34dc7c2f BB |
103 | return (ds->ds_dir->dd_pool); |
104 | else | |
428870ff | 105 | return (spa_get_dsl(os->os_spa)); |
34dc7c2f BB |
106 | } |
107 | ||
108 | dsl_dataset_t * | |
109 | dmu_objset_ds(objset_t *os) | |
110 | { | |
428870ff | 111 | return (os->os_dsl_dataset); |
34dc7c2f BB |
112 | } |
113 | ||
114 | dmu_objset_type_t | |
115 | dmu_objset_type(objset_t *os) | |
116 | { | |
428870ff | 117 | return (os->os_phys->os_type); |
34dc7c2f BB |
118 | } |
119 | ||
120 | void | |
121 | dmu_objset_name(objset_t *os, char *buf) | |
122 | { | |
428870ff | 123 | dsl_dataset_name(os->os_dsl_dataset, buf); |
34dc7c2f BB |
124 | } |
125 | ||
126 | uint64_t | |
127 | dmu_objset_id(objset_t *os) | |
128 | { | |
428870ff | 129 | dsl_dataset_t *ds = os->os_dsl_dataset; |
34dc7c2f BB |
130 | |
131 | return (ds ? ds->ds_object : 0); | |
132 | } | |
133 | ||
faf0f58c | 134 | zfs_sync_type_t |
428870ff BB |
135 | dmu_objset_syncprop(objset_t *os) |
136 | { | |
137 | return (os->os_sync); | |
138 | } | |
139 | ||
faf0f58c | 140 | zfs_logbias_op_t |
428870ff BB |
141 | dmu_objset_logbias(objset_t *os) |
142 | { | |
143 | return (os->os_logbias); | |
144 | } | |
145 | ||
34dc7c2f BB |
146 | static void |
147 | checksum_changed_cb(void *arg, uint64_t newval) | |
148 | { | |
428870ff | 149 | objset_t *os = arg; |
34dc7c2f BB |
150 | |
151 | /* | |
152 | * Inheritance should have been done by now. | |
153 | */ | |
154 | ASSERT(newval != ZIO_CHECKSUM_INHERIT); | |
155 | ||
428870ff | 156 | os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE); |
34dc7c2f BB |
157 | } |
158 | ||
159 | static void | |
160 | compression_changed_cb(void *arg, uint64_t newval) | |
161 | { | |
428870ff | 162 | objset_t *os = arg; |
34dc7c2f BB |
163 | |
164 | /* | |
165 | * Inheritance and range checking should have been done by now. | |
166 | */ | |
167 | ASSERT(newval != ZIO_COMPRESS_INHERIT); | |
168 | ||
99197f03 JG |
169 | os->os_compress = zio_compress_select(os->os_spa, newval, |
170 | ZIO_COMPRESS_ON); | |
34dc7c2f BB |
171 | } |
172 | ||
173 | static void | |
174 | copies_changed_cb(void *arg, uint64_t newval) | |
175 | { | |
428870ff | 176 | objset_t *os = arg; |
34dc7c2f BB |
177 | |
178 | /* | |
179 | * Inheritance and range checking should have been done by now. | |
180 | */ | |
181 | ASSERT(newval > 0); | |
428870ff | 182 | ASSERT(newval <= spa_max_replication(os->os_spa)); |
34dc7c2f | 183 | |
428870ff BB |
184 | os->os_copies = newval; |
185 | } | |
186 | ||
187 | static void | |
188 | dedup_changed_cb(void *arg, uint64_t newval) | |
189 | { | |
190 | objset_t *os = arg; | |
191 | spa_t *spa = os->os_spa; | |
192 | enum zio_checksum checksum; | |
193 | ||
194 | /* | |
195 | * Inheritance should have been done by now. | |
196 | */ | |
197 | ASSERT(newval != ZIO_CHECKSUM_INHERIT); | |
198 | ||
199 | checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF); | |
200 | ||
201 | os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK; | |
202 | os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY); | |
34dc7c2f BB |
203 | } |
204 | ||
b128c09f BB |
205 | static void |
206 | primary_cache_changed_cb(void *arg, uint64_t newval) | |
207 | { | |
428870ff | 208 | objset_t *os = arg; |
b128c09f BB |
209 | |
210 | /* | |
211 | * Inheritance and range checking should have been done by now. | |
212 | */ | |
213 | ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE || | |
214 | newval == ZFS_CACHE_METADATA); | |
215 | ||
428870ff | 216 | os->os_primary_cache = newval; |
b128c09f BB |
217 | } |
218 | ||
219 | static void | |
220 | secondary_cache_changed_cb(void *arg, uint64_t newval) | |
221 | { | |
428870ff | 222 | objset_t *os = arg; |
b128c09f BB |
223 | |
224 | /* | |
225 | * Inheritance and range checking should have been done by now. | |
226 | */ | |
227 | ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE || | |
228 | newval == ZFS_CACHE_METADATA); | |
229 | ||
428870ff BB |
230 | os->os_secondary_cache = newval; |
231 | } | |
232 | ||
233 | static void | |
234 | sync_changed_cb(void *arg, uint64_t newval) | |
235 | { | |
236 | objset_t *os = arg; | |
237 | ||
238 | /* | |
239 | * Inheritance and range checking should have been done by now. | |
240 | */ | |
241 | ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS || | |
242 | newval == ZFS_SYNC_DISABLED); | |
243 | ||
244 | os->os_sync = newval; | |
245 | if (os->os_zil) | |
246 | zil_set_sync(os->os_zil, newval); | |
247 | } | |
248 | ||
faf0f58c MA |
249 | static void |
250 | redundant_metadata_changed_cb(void *arg, uint64_t newval) | |
251 | { | |
252 | objset_t *os = arg; | |
253 | ||
254 | /* | |
255 | * Inheritance and range checking should have been done by now. | |
256 | */ | |
257 | ASSERT(newval == ZFS_REDUNDANT_METADATA_ALL || | |
258 | newval == ZFS_REDUNDANT_METADATA_MOST); | |
259 | ||
260 | os->os_redundant_metadata = newval; | |
261 | } | |
262 | ||
428870ff BB |
263 | static void |
264 | logbias_changed_cb(void *arg, uint64_t newval) | |
265 | { | |
266 | objset_t *os = arg; | |
267 | ||
268 | ASSERT(newval == ZFS_LOGBIAS_LATENCY || | |
269 | newval == ZFS_LOGBIAS_THROUGHPUT); | |
270 | os->os_logbias = newval; | |
271 | if (os->os_zil) | |
272 | zil_set_logbias(os->os_zil, newval); | |
b128c09f BB |
273 | } |
274 | ||
f1512ee6 MA |
275 | static void |
276 | recordsize_changed_cb(void *arg, uint64_t newval) | |
277 | { | |
278 | objset_t *os = arg; | |
279 | ||
280 | os->os_recordsize = newval; | |
281 | } | |
282 | ||
34dc7c2f BB |
283 | void |
284 | dmu_objset_byteswap(void *buf, size_t size) | |
285 | { | |
286 | objset_phys_t *osp = buf; | |
287 | ||
9babb374 | 288 | ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t)); |
34dc7c2f BB |
289 | dnode_byteswap(&osp->os_meta_dnode); |
290 | byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t)); | |
291 | osp->os_type = BSWAP_64(osp->os_type); | |
9babb374 BB |
292 | osp->os_flags = BSWAP_64(osp->os_flags); |
293 | if (size == sizeof (objset_phys_t)) { | |
294 | dnode_byteswap(&osp->os_userused_dnode); | |
295 | dnode_byteswap(&osp->os_groupused_dnode); | |
296 | } | |
34dc7c2f BB |
297 | } |
298 | ||
299 | int | |
300 | dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, | |
428870ff | 301 | objset_t **osp) |
34dc7c2f | 302 | { |
428870ff | 303 | objset_t *os; |
b128c09f | 304 | int i, err; |
34dc7c2f BB |
305 | |
306 | ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock)); | |
307 | ||
79c76d5b | 308 | os = kmem_zalloc(sizeof (objset_t), KM_SLEEP); |
428870ff BB |
309 | os->os_dsl_dataset = ds; |
310 | os->os_spa = spa; | |
311 | os->os_rootbp = bp; | |
312 | if (!BP_IS_HOLE(os->os_rootbp)) { | |
2a432414 | 313 | arc_flags_t aflags = ARC_FLAG_WAIT; |
5dbd68a3 | 314 | zbookmark_phys_t zb; |
428870ff BB |
315 | SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET, |
316 | ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); | |
317 | ||
318 | if (DMU_OS_IS_L2CACHEABLE(os)) | |
2a432414 | 319 | aflags |= ARC_FLAG_L2CACHE; |
3a17a7a9 | 320 | if (DMU_OS_IS_L2COMPRESSIBLE(os)) |
2a432414 | 321 | aflags |= ARC_FLAG_L2COMPRESS; |
34dc7c2f | 322 | |
428870ff | 323 | dprintf_bp(os->os_rootbp, "reading %s", ""); |
294f6806 | 324 | err = arc_read(NULL, spa, os->os_rootbp, |
428870ff | 325 | arc_getbuf_func, &os->os_phys_buf, |
34dc7c2f | 326 | ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb); |
13fe0198 | 327 | if (err != 0) { |
428870ff | 328 | kmem_free(os, sizeof (objset_t)); |
b128c09f BB |
329 | /* convert checksum errors into IO errors */ |
330 | if (err == ECKSUM) | |
2e528b49 | 331 | err = SET_ERROR(EIO); |
34dc7c2f BB |
332 | return (err); |
333 | } | |
9babb374 BB |
334 | |
335 | /* Increase the blocksize if we are permitted. */ | |
336 | if (spa_version(spa) >= SPA_VERSION_USERSPACE && | |
428870ff | 337 | arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) { |
9babb374 | 338 | arc_buf_t *buf = arc_buf_alloc(spa, |
428870ff | 339 | sizeof (objset_phys_t), &os->os_phys_buf, |
9babb374 BB |
340 | ARC_BUFC_METADATA); |
341 | bzero(buf->b_data, sizeof (objset_phys_t)); | |
428870ff BB |
342 | bcopy(os->os_phys_buf->b_data, buf->b_data, |
343 | arc_buf_size(os->os_phys_buf)); | |
344 | (void) arc_buf_remove_ref(os->os_phys_buf, | |
345 | &os->os_phys_buf); | |
346 | os->os_phys_buf = buf; | |
9babb374 BB |
347 | } |
348 | ||
428870ff BB |
349 | os->os_phys = os->os_phys_buf->b_data; |
350 | os->os_flags = os->os_phys->os_flags; | |
34dc7c2f | 351 | } else { |
9babb374 BB |
352 | int size = spa_version(spa) >= SPA_VERSION_USERSPACE ? |
353 | sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE; | |
428870ff BB |
354 | os->os_phys_buf = arc_buf_alloc(spa, size, |
355 | &os->os_phys_buf, ARC_BUFC_METADATA); | |
356 | os->os_phys = os->os_phys_buf->b_data; | |
357 | bzero(os->os_phys, size); | |
34dc7c2f BB |
358 | } |
359 | ||
360 | /* | |
361 | * Note: the changed_cb will be called once before the register | |
362 | * func returns, thus changing the checksum/compression from the | |
b128c09f BB |
363 | * default (fletcher2/off). Snapshots don't need to know about |
364 | * checksum/compression/copies. | |
34dc7c2f | 365 | */ |
9b67f605 | 366 | if (ds != NULL) { |
13fe0198 MA |
367 | err = dsl_prop_register(ds, |
368 | zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE), | |
428870ff | 369 | primary_cache_changed_cb, os); |
13fe0198 MA |
370 | if (err == 0) { |
371 | err = dsl_prop_register(ds, | |
372 | zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE), | |
428870ff | 373 | secondary_cache_changed_cb, os); |
13fe0198 | 374 | } |
0c66c32d | 375 | if (!ds->ds_is_snapshot) { |
13fe0198 MA |
376 | if (err == 0) { |
377 | err = dsl_prop_register(ds, | |
378 | zfs_prop_to_name(ZFS_PROP_CHECKSUM), | |
428870ff | 379 | checksum_changed_cb, os); |
13fe0198 MA |
380 | } |
381 | if (err == 0) { | |
382 | err = dsl_prop_register(ds, | |
383 | zfs_prop_to_name(ZFS_PROP_COMPRESSION), | |
428870ff | 384 | compression_changed_cb, os); |
13fe0198 MA |
385 | } |
386 | if (err == 0) { | |
387 | err = dsl_prop_register(ds, | |
388 | zfs_prop_to_name(ZFS_PROP_COPIES), | |
428870ff | 389 | copies_changed_cb, os); |
13fe0198 MA |
390 | } |
391 | if (err == 0) { | |
392 | err = dsl_prop_register(ds, | |
393 | zfs_prop_to_name(ZFS_PROP_DEDUP), | |
428870ff | 394 | dedup_changed_cb, os); |
13fe0198 MA |
395 | } |
396 | if (err == 0) { | |
397 | err = dsl_prop_register(ds, | |
398 | zfs_prop_to_name(ZFS_PROP_LOGBIAS), | |
428870ff | 399 | logbias_changed_cb, os); |
13fe0198 MA |
400 | } |
401 | if (err == 0) { | |
402 | err = dsl_prop_register(ds, | |
403 | zfs_prop_to_name(ZFS_PROP_SYNC), | |
428870ff | 404 | sync_changed_cb, os); |
13fe0198 | 405 | } |
faf0f58c MA |
406 | if (err == 0) { |
407 | err = dsl_prop_register(ds, | |
408 | zfs_prop_to_name( | |
409 | ZFS_PROP_REDUNDANT_METADATA), | |
410 | redundant_metadata_changed_cb, os); | |
411 | } | |
f1512ee6 MA |
412 | if (err == 0) { |
413 | err = dsl_prop_register(ds, | |
414 | zfs_prop_to_name(ZFS_PROP_RECORDSIZE), | |
415 | recordsize_changed_cb, os); | |
416 | } | |
b128c09f | 417 | } |
13fe0198 | 418 | if (err != 0) { |
428870ff | 419 | VERIFY(arc_buf_remove_ref(os->os_phys_buf, |
13fe0198 | 420 | &os->os_phys_buf)); |
428870ff | 421 | kmem_free(os, sizeof (objset_t)); |
34dc7c2f BB |
422 | return (err); |
423 | } | |
9b67f605 | 424 | } else { |
34dc7c2f | 425 | /* It's the meta-objset. */ |
428870ff | 426 | os->os_checksum = ZIO_CHECKSUM_FLETCHER_4; |
99197f03 | 427 | os->os_compress = ZIO_COMPRESS_ON; |
428870ff BB |
428 | os->os_copies = spa_max_replication(spa); |
429 | os->os_dedup_checksum = ZIO_CHECKSUM_OFF; | |
faf0f58c MA |
430 | os->os_dedup_verify = B_FALSE; |
431 | os->os_logbias = ZFS_LOGBIAS_LATENCY; | |
432 | os->os_sync = ZFS_SYNC_STANDARD; | |
428870ff BB |
433 | os->os_primary_cache = ZFS_CACHE_ALL; |
434 | os->os_secondary_cache = ZFS_CACHE_ALL; | |
34dc7c2f BB |
435 | } |
436 | ||
0c66c32d | 437 | if (ds == NULL || !ds->ds_is_snapshot) |
572e2857 | 438 | os->os_zil_header = os->os_phys->os_zil_header; |
428870ff | 439 | os->os_zil = zil_alloc(os, &os->os_zil_header); |
34dc7c2f BB |
440 | |
441 | for (i = 0; i < TXG_SIZE; i++) { | |
428870ff | 442 | list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t), |
34dc7c2f | 443 | offsetof(dnode_t, dn_dirty_link[i])); |
428870ff | 444 | list_create(&os->os_free_dnodes[i], sizeof (dnode_t), |
34dc7c2f BB |
445 | offsetof(dnode_t, dn_dirty_link[i])); |
446 | } | |
428870ff | 447 | list_create(&os->os_dnodes, sizeof (dnode_t), |
34dc7c2f | 448 | offsetof(dnode_t, dn_link)); |
428870ff | 449 | list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t), |
34dc7c2f BB |
450 | offsetof(dmu_buf_impl_t, db_link)); |
451 | ||
0c66c32d JG |
452 | list_link_init(&os->os_evicting_node); |
453 | ||
428870ff BB |
454 | mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL); |
455 | mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL); | |
456 | mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL); | |
457 | ||
0c66c32d JG |
458 | dnode_special_open(os, &os->os_phys->os_meta_dnode, |
459 | DMU_META_DNODE_OBJECT, &os->os_meta_dnode); | |
428870ff | 460 | if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) { |
0c66c32d JG |
461 | dnode_special_open(os, &os->os_phys->os_userused_dnode, |
462 | DMU_USERUSED_OBJECT, &os->os_userused_dnode); | |
463 | dnode_special_open(os, &os->os_phys->os_groupused_dnode, | |
464 | DMU_GROUPUSED_OBJECT, &os->os_groupused_dnode); | |
9babb374 | 465 | } |
34dc7c2f | 466 | |
428870ff | 467 | *osp = os; |
34dc7c2f BB |
468 | return (0); |
469 | } | |
470 | ||
428870ff BB |
471 | int |
472 | dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp) | |
34dc7c2f | 473 | { |
428870ff | 474 | int err = 0; |
34dc7c2f BB |
475 | |
476 | mutex_enter(&ds->ds_opening_lock); | |
9b67f605 MA |
477 | if (ds->ds_objset == NULL) { |
478 | objset_t *os; | |
34dc7c2f | 479 | err = dmu_objset_open_impl(dsl_dataset_get_spa(ds), |
9b67f605 MA |
480 | ds, dsl_dataset_get_blkptr(ds), &os); |
481 | ||
482 | if (err == 0) { | |
483 | mutex_enter(&ds->ds_lock); | |
484 | ASSERT(ds->ds_objset == NULL); | |
485 | ds->ds_objset = os; | |
486 | mutex_exit(&ds->ds_lock); | |
487 | } | |
34dc7c2f | 488 | } |
9b67f605 | 489 | *osp = ds->ds_objset; |
34dc7c2f | 490 | mutex_exit(&ds->ds_opening_lock); |
428870ff | 491 | return (err); |
34dc7c2f BB |
492 | } |
493 | ||
13fe0198 MA |
494 | /* |
495 | * Holds the pool while the objset is held. Therefore only one objset | |
496 | * can be held at a time. | |
497 | */ | |
34dc7c2f | 498 | int |
428870ff | 499 | dmu_objset_hold(const char *name, void *tag, objset_t **osp) |
34dc7c2f | 500 | { |
13fe0198 | 501 | dsl_pool_t *dp; |
428870ff | 502 | dsl_dataset_t *ds; |
34dc7c2f BB |
503 | int err; |
504 | ||
13fe0198 MA |
505 | err = dsl_pool_hold(name, tag, &dp); |
506 | if (err != 0) | |
507 | return (err); | |
508 | err = dsl_dataset_hold(dp, name, tag, &ds); | |
509 | if (err != 0) { | |
510 | dsl_pool_rele(dp, tag); | |
428870ff | 511 | return (err); |
13fe0198 | 512 | } |
428870ff BB |
513 | |
514 | err = dmu_objset_from_ds(ds, osp); | |
13fe0198 | 515 | if (err != 0) { |
428870ff | 516 | dsl_dataset_rele(ds, tag); |
13fe0198 MA |
517 | dsl_pool_rele(dp, tag); |
518 | } | |
428870ff | 519 | |
34dc7c2f BB |
520 | return (err); |
521 | } | |
522 | ||
9c43027b AJ |
523 | static int |
524 | dmu_objset_own_impl(dsl_dataset_t *ds, dmu_objset_type_t type, | |
525 | boolean_t readonly, void *tag, objset_t **osp) | |
526 | { | |
527 | int err; | |
528 | ||
529 | err = dmu_objset_from_ds(ds, osp); | |
530 | if (err != 0) { | |
531 | dsl_dataset_disown(ds, tag); | |
532 | } else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) { | |
533 | dsl_dataset_disown(ds, tag); | |
534 | return (SET_ERROR(EINVAL)); | |
535 | } else if (!readonly && dsl_dataset_is_snapshot(ds)) { | |
536 | dsl_dataset_disown(ds, tag); | |
537 | return (SET_ERROR(EROFS)); | |
538 | } | |
539 | return (err); | |
540 | } | |
541 | ||
13fe0198 MA |
542 | /* |
543 | * dsl_pool must not be held when this is called. | |
544 | * Upon successful return, there will be a longhold on the dataset, | |
545 | * and the dsl_pool will not be held. | |
546 | */ | |
34dc7c2f | 547 | int |
428870ff BB |
548 | dmu_objset_own(const char *name, dmu_objset_type_t type, |
549 | boolean_t readonly, void *tag, objset_t **osp) | |
34dc7c2f | 550 | { |
13fe0198 | 551 | dsl_pool_t *dp; |
34dc7c2f BB |
552 | dsl_dataset_t *ds; |
553 | int err; | |
554 | ||
13fe0198 MA |
555 | err = dsl_pool_hold(name, FTAG, &dp); |
556 | if (err != 0) | |
557 | return (err); | |
558 | err = dsl_dataset_own(dp, name, tag, &ds); | |
559 | if (err != 0) { | |
560 | dsl_pool_rele(dp, FTAG); | |
34dc7c2f | 561 | return (err); |
13fe0198 | 562 | } |
9c43027b | 563 | err = dmu_objset_own_impl(ds, type, readonly, tag, osp); |
13fe0198 | 564 | dsl_pool_rele(dp, FTAG); |
9c43027b | 565 | |
34dc7c2f BB |
566 | return (err); |
567 | } | |
568 | ||
9c43027b AJ |
569 | int |
570 | dmu_objset_own_obj(dsl_pool_t *dp, uint64_t obj, dmu_objset_type_t type, | |
571 | boolean_t readonly, void *tag, objset_t **osp) | |
572 | { | |
573 | dsl_dataset_t *ds; | |
574 | int err; | |
575 | ||
576 | err = dsl_dataset_own_obj(dp, obj, tag, &ds); | |
577 | if (err != 0) | |
578 | return (err); | |
579 | ||
580 | return (dmu_objset_own_impl(ds, type, readonly, tag, osp)); | |
581 | } | |
582 | ||
34dc7c2f | 583 | void |
428870ff | 584 | dmu_objset_rele(objset_t *os, void *tag) |
34dc7c2f | 585 | { |
13fe0198 | 586 | dsl_pool_t *dp = dmu_objset_pool(os); |
428870ff | 587 | dsl_dataset_rele(os->os_dsl_dataset, tag); |
13fe0198 | 588 | dsl_pool_rele(dp, tag); |
428870ff | 589 | } |
b128c09f | 590 | |
831baf06 KW |
591 | /* |
592 | * When we are called, os MUST refer to an objset associated with a dataset | |
593 | * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner | |
594 | * == tag. We will then release and reacquire ownership of the dataset while | |
595 | * holding the pool config_rwlock to avoid intervening namespace or ownership | |
596 | * changes may occur. | |
597 | * | |
598 | * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to | |
599 | * release the hold on its dataset and acquire a new one on the dataset of the | |
600 | * same name so that it can be partially torn down and reconstructed. | |
601 | */ | |
602 | void | |
603 | dmu_objset_refresh_ownership(objset_t *os, void *tag) | |
604 | { | |
605 | dsl_pool_t *dp; | |
606 | dsl_dataset_t *ds, *newds; | |
607 | char name[MAXNAMELEN]; | |
608 | ||
609 | ds = os->os_dsl_dataset; | |
610 | VERIFY3P(ds, !=, NULL); | |
611 | VERIFY3P(ds->ds_owner, ==, tag); | |
612 | VERIFY(dsl_dataset_long_held(ds)); | |
613 | ||
614 | dsl_dataset_name(ds, name); | |
615 | dp = dmu_objset_pool(os); | |
616 | dsl_pool_config_enter(dp, FTAG); | |
617 | dmu_objset_disown(os, tag); | |
618 | VERIFY0(dsl_dataset_own(dp, name, tag, &newds)); | |
619 | VERIFY3P(newds, ==, os->os_dsl_dataset); | |
620 | dsl_pool_config_exit(dp, FTAG); | |
621 | } | |
622 | ||
428870ff BB |
623 | void |
624 | dmu_objset_disown(objset_t *os, void *tag) | |
625 | { | |
626 | dsl_dataset_disown(os->os_dsl_dataset, tag); | |
34dc7c2f BB |
627 | } |
628 | ||
13fe0198 | 629 | void |
34dc7c2f BB |
630 | dmu_objset_evict_dbufs(objset_t *os) |
631 | { | |
0c66c32d | 632 | dnode_t *dn_marker; |
34dc7c2f BB |
633 | dnode_t *dn; |
634 | ||
0c66c32d | 635 | dn_marker = kmem_alloc(sizeof (dnode_t), KM_SLEEP); |
34dc7c2f | 636 | |
0c66c32d JG |
637 | mutex_enter(&os->os_lock); |
638 | dn = list_head(&os->os_dnodes); | |
639 | while (dn != NULL) { | |
640 | /* | |
641 | * Skip dnodes without holds. We have to do this dance | |
642 | * because dnode_add_ref() only works if there is already a | |
643 | * hold. If the dnode has no holds, then it has no dbufs. | |
644 | */ | |
645 | if (dnode_add_ref(dn, FTAG)) { | |
646 | list_insert_after(&os->os_dnodes, dn, dn_marker); | |
647 | mutex_exit(&os->os_lock); | |
34dc7c2f | 648 | |
0c66c32d JG |
649 | dnode_evict_dbufs(dn); |
650 | dnode_rele(dn, FTAG); | |
34dc7c2f | 651 | |
0c66c32d JG |
652 | mutex_enter(&os->os_lock); |
653 | dn = list_next(&os->os_dnodes, dn_marker); | |
654 | list_remove(&os->os_dnodes, dn_marker); | |
655 | } else { | |
656 | dn = list_next(&os->os_dnodes, dn); | |
657 | } | |
658 | } | |
659 | mutex_exit(&os->os_lock); | |
34dc7c2f | 660 | |
0c66c32d | 661 | kmem_free(dn_marker, sizeof (dnode_t)); |
34dc7c2f | 662 | |
0c66c32d JG |
663 | if (DMU_USERUSED_DNODE(os) != NULL) { |
664 | dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os)); | |
665 | dnode_evict_dbufs(DMU_USERUSED_DNODE(os)); | |
34dc7c2f | 666 | } |
0c66c32d | 667 | dnode_evict_dbufs(DMU_META_DNODE(os)); |
34dc7c2f BB |
668 | } |
669 | ||
0c66c32d JG |
670 | /* |
671 | * Objset eviction processing is split into into two pieces. | |
672 | * The first marks the objset as evicting, evicts any dbufs that | |
673 | * have a refcount of zero, and then queues up the objset for the | |
674 | * second phase of eviction. Once os->os_dnodes has been cleared by | |
675 | * dnode_buf_pageout()->dnode_destroy(), the second phase is executed. | |
676 | * The second phase closes the special dnodes, dequeues the objset from | |
677 | * the list of those undergoing eviction, and finally frees the objset. | |
678 | * | |
679 | * NOTE: Due to asynchronous eviction processing (invocation of | |
680 | * dnode_buf_pageout()), it is possible for the meta dnode for the | |
681 | * objset to have no holds even though os->os_dnodes is not empty. | |
682 | */ | |
34dc7c2f | 683 | void |
428870ff | 684 | dmu_objset_evict(objset_t *os) |
34dc7c2f | 685 | { |
d6320ddb | 686 | int t; |
34dc7c2f | 687 | |
6f1ffb06 MA |
688 | dsl_dataset_t *ds = os->os_dsl_dataset; |
689 | ||
d6320ddb | 690 | for (t = 0; t < TXG_SIZE; t++) |
428870ff | 691 | ASSERT(!dmu_objset_is_dirty(os, t)); |
34dc7c2f | 692 | |
0eb21616 JG |
693 | if (ds) |
694 | dsl_prop_unregister_all(ds, os); | |
34dc7c2f | 695 | |
428870ff BB |
696 | if (os->os_sa) |
697 | sa_tear_down(os); | |
698 | ||
13fe0198 | 699 | dmu_objset_evict_dbufs(os); |
34dc7c2f | 700 | |
0c66c32d JG |
701 | mutex_enter(&os->os_lock); |
702 | spa_evicting_os_register(os->os_spa, os); | |
703 | if (list_is_empty(&os->os_dnodes)) { | |
704 | mutex_exit(&os->os_lock); | |
705 | dmu_objset_evict_done(os); | |
706 | } else { | |
707 | mutex_exit(&os->os_lock); | |
708 | } | |
709 | } | |
710 | ||
711 | void | |
712 | dmu_objset_evict_done(objset_t *os) | |
713 | { | |
714 | ASSERT3P(list_head(&os->os_dnodes), ==, NULL); | |
715 | ||
572e2857 BB |
716 | dnode_special_close(&os->os_meta_dnode); |
717 | if (DMU_USERUSED_DNODE(os)) { | |
718 | dnode_special_close(&os->os_userused_dnode); | |
719 | dnode_special_close(&os->os_groupused_dnode); | |
9babb374 | 720 | } |
428870ff BB |
721 | zil_free(os->os_zil); |
722 | ||
13fe0198 | 723 | VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf)); |
572e2857 BB |
724 | |
725 | /* | |
726 | * This is a barrier to prevent the objset from going away in | |
727 | * dnode_move() until we can safely ensure that the objset is still in | |
728 | * use. We consider the objset valid before the barrier and invalid | |
729 | * after the barrier. | |
730 | */ | |
731 | rw_enter(&os_lock, RW_READER); | |
732 | rw_exit(&os_lock); | |
733 | ||
428870ff BB |
734 | mutex_destroy(&os->os_lock); |
735 | mutex_destroy(&os->os_obj_lock); | |
736 | mutex_destroy(&os->os_user_ptr_lock); | |
0c66c32d | 737 | spa_evicting_os_deregister(os->os_spa, os); |
428870ff BB |
738 | kmem_free(os, sizeof (objset_t)); |
739 | } | |
9babb374 | 740 | |
428870ff BB |
741 | timestruc_t |
742 | dmu_objset_snap_cmtime(objset_t *os) | |
743 | { | |
744 | return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir)); | |
34dc7c2f BB |
745 | } |
746 | ||
747 | /* called from dsl for meta-objset */ | |
428870ff | 748 | objset_t * |
34dc7c2f BB |
749 | dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, |
750 | dmu_objset_type_t type, dmu_tx_t *tx) | |
751 | { | |
428870ff | 752 | objset_t *os; |
34dc7c2f BB |
753 | dnode_t *mdn; |
754 | ||
755 | ASSERT(dmu_tx_is_syncing(tx)); | |
13fe0198 | 756 | |
572e2857 | 757 | if (ds != NULL) |
13fe0198 | 758 | VERIFY0(dmu_objset_from_ds(ds, &os)); |
572e2857 | 759 | else |
13fe0198 | 760 | VERIFY0(dmu_objset_open_impl(spa, NULL, bp, &os)); |
572e2857 BB |
761 | |
762 | mdn = DMU_META_DNODE(os); | |
34dc7c2f BB |
763 | |
764 | dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT, | |
765 | DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx); | |
766 | ||
767 | /* | |
768 | * We don't want to have to increase the meta-dnode's nlevels | |
769 | * later, because then we could do it in quescing context while | |
770 | * we are also accessing it in open context. | |
771 | * | |
772 | * This precaution is not necessary for the MOS (ds == NULL), | |
773 | * because the MOS is only updated in syncing context. | |
774 | * This is most fortunate: the MOS is the only objset that | |
775 | * needs to be synced multiple times as spa_sync() iterates | |
776 | * to convergence, so minimizing its dn_nlevels matters. | |
777 | */ | |
778 | if (ds != NULL) { | |
779 | int levels = 1; | |
780 | ||
781 | /* | |
782 | * Determine the number of levels necessary for the meta-dnode | |
783 | * to contain DN_MAX_OBJECT dnodes. | |
784 | */ | |
785 | while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift + | |
786 | (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) < | |
787 | DN_MAX_OBJECT * sizeof (dnode_phys_t)) | |
788 | levels++; | |
789 | ||
790 | mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] = | |
791 | mdn->dn_nlevels = levels; | |
792 | } | |
793 | ||
794 | ASSERT(type != DMU_OST_NONE); | |
795 | ASSERT(type != DMU_OST_ANY); | |
796 | ASSERT(type < DMU_OST_NUMTYPES); | |
428870ff BB |
797 | os->os_phys->os_type = type; |
798 | if (dmu_objset_userused_enabled(os)) { | |
799 | os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE; | |
800 | os->os_flags = os->os_phys->os_flags; | |
9babb374 | 801 | } |
34dc7c2f BB |
802 | |
803 | dsl_dataset_dirty(ds, tx); | |
804 | ||
428870ff | 805 | return (os); |
34dc7c2f BB |
806 | } |
807 | ||
13fe0198 MA |
808 | typedef struct dmu_objset_create_arg { |
809 | const char *doca_name; | |
810 | cred_t *doca_cred; | |
811 | void (*doca_userfunc)(objset_t *os, void *arg, | |
812 | cred_t *cr, dmu_tx_t *tx); | |
813 | void *doca_userarg; | |
814 | dmu_objset_type_t doca_type; | |
815 | uint64_t doca_flags; | |
816 | } dmu_objset_create_arg_t; | |
34dc7c2f BB |
817 | |
818 | /*ARGSUSED*/ | |
819 | static int | |
13fe0198 | 820 | dmu_objset_create_check(void *arg, dmu_tx_t *tx) |
34dc7c2f | 821 | { |
13fe0198 MA |
822 | dmu_objset_create_arg_t *doca = arg; |
823 | dsl_pool_t *dp = dmu_tx_pool(tx); | |
824 | dsl_dir_t *pdd; | |
825 | const char *tail; | |
826 | int error; | |
34dc7c2f | 827 | |
13fe0198 | 828 | if (strchr(doca->doca_name, '@') != NULL) |
2e528b49 | 829 | return (SET_ERROR(EINVAL)); |
34dc7c2f | 830 | |
13fe0198 MA |
831 | error = dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail); |
832 | if (error != 0) | |
833 | return (error); | |
834 | if (tail == NULL) { | |
835 | dsl_dir_rele(pdd, FTAG); | |
2e528b49 | 836 | return (SET_ERROR(EEXIST)); |
34dc7c2f | 837 | } |
788eb90c JJ |
838 | error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL, |
839 | doca->doca_cred); | |
13fe0198 | 840 | dsl_dir_rele(pdd, FTAG); |
34dc7c2f | 841 | |
788eb90c | 842 | return (error); |
34dc7c2f BB |
843 | } |
844 | ||
845 | static void | |
13fe0198 | 846 | dmu_objset_create_sync(void *arg, dmu_tx_t *tx) |
34dc7c2f | 847 | { |
13fe0198 MA |
848 | dmu_objset_create_arg_t *doca = arg; |
849 | dsl_pool_t *dp = dmu_tx_pool(tx); | |
850 | dsl_dir_t *pdd; | |
851 | const char *tail; | |
6f1ffb06 | 852 | dsl_dataset_t *ds; |
13fe0198 | 853 | uint64_t obj; |
6f1ffb06 | 854 | blkptr_t *bp; |
13fe0198 | 855 | objset_t *os; |
34dc7c2f | 856 | |
13fe0198 | 857 | VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail)); |
34dc7c2f | 858 | |
13fe0198 MA |
859 | obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags, |
860 | doca->doca_cred, tx); | |
34dc7c2f | 861 | |
13fe0198 | 862 | VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds)); |
6f1ffb06 | 863 | bp = dsl_dataset_get_blkptr(ds); |
13fe0198 MA |
864 | os = dmu_objset_create_impl(pdd->dd_pool->dp_spa, |
865 | ds, bp, doca->doca_type, tx); | |
34dc7c2f | 866 | |
13fe0198 MA |
867 | if (doca->doca_userfunc != NULL) { |
868 | doca->doca_userfunc(os, doca->doca_userarg, | |
869 | doca->doca_cred, tx); | |
34dc7c2f BB |
870 | } |
871 | ||
13fe0198 | 872 | spa_history_log_internal_ds(ds, "create", tx, ""); |
a0bd735a BP |
873 | zvol_create_minors(dp->dp_spa, doca->doca_name, B_TRUE); |
874 | ||
6f1ffb06 | 875 | dsl_dataset_rele(ds, FTAG); |
13fe0198 | 876 | dsl_dir_rele(pdd, FTAG); |
34dc7c2f BB |
877 | } |
878 | ||
879 | int | |
428870ff | 880 | dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags, |
34dc7c2f BB |
881 | void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg) |
882 | { | |
13fe0198 | 883 | dmu_objset_create_arg_t doca; |
34dc7c2f | 884 | |
13fe0198 MA |
885 | doca.doca_name = name; |
886 | doca.doca_cred = CRED(); | |
887 | doca.doca_flags = flags; | |
888 | doca.doca_userfunc = func; | |
889 | doca.doca_userarg = arg; | |
890 | doca.doca_type = type; | |
34dc7c2f | 891 | |
13fe0198 | 892 | return (dsl_sync_task(name, |
3d45fdd6 MA |
893 | dmu_objset_create_check, dmu_objset_create_sync, &doca, |
894 | 5, ZFS_SPACE_CHECK_NORMAL)); | |
34dc7c2f BB |
895 | } |
896 | ||
13fe0198 MA |
897 | typedef struct dmu_objset_clone_arg { |
898 | const char *doca_clone; | |
899 | const char *doca_origin; | |
900 | cred_t *doca_cred; | |
901 | } dmu_objset_clone_arg_t; | |
902 | ||
903 | /*ARGSUSED*/ | |
904 | static int | |
905 | dmu_objset_clone_check(void *arg, dmu_tx_t *tx) | |
34dc7c2f | 906 | { |
13fe0198 | 907 | dmu_objset_clone_arg_t *doca = arg; |
428870ff BB |
908 | dsl_dir_t *pdd; |
909 | const char *tail; | |
13fe0198 MA |
910 | int error; |
911 | dsl_dataset_t *origin; | |
912 | dsl_pool_t *dp = dmu_tx_pool(tx); | |
34dc7c2f | 913 | |
13fe0198 | 914 | if (strchr(doca->doca_clone, '@') != NULL) |
2e528b49 | 915 | return (SET_ERROR(EINVAL)); |
13fe0198 MA |
916 | |
917 | error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail); | |
918 | if (error != 0) | |
919 | return (error); | |
428870ff | 920 | if (tail == NULL) { |
13fe0198 | 921 | dsl_dir_rele(pdd, FTAG); |
2e528b49 | 922 | return (SET_ERROR(EEXIST)); |
34dc7c2f | 923 | } |
1cddb8c9 | 924 | |
788eb90c JJ |
925 | error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL, |
926 | doca->doca_cred); | |
927 | if (error != 0) { | |
928 | dsl_dir_rele(pdd, FTAG); | |
929 | return (SET_ERROR(EDQUOT)); | |
930 | } | |
13fe0198 | 931 | dsl_dir_rele(pdd, FTAG); |
428870ff | 932 | |
13fe0198 MA |
933 | error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin); |
934 | if (error != 0) | |
572e2857 BB |
935 | return (error); |
936 | ||
13fe0198 | 937 | /* You can only clone snapshots, not the head datasets. */ |
0c66c32d | 938 | if (!origin->ds_is_snapshot) { |
13fe0198 | 939 | dsl_dataset_rele(origin, FTAG); |
2e528b49 | 940 | return (SET_ERROR(EINVAL)); |
428870ff | 941 | } |
13fe0198 | 942 | dsl_dataset_rele(origin, FTAG); |
572e2857 | 943 | |
13fe0198 | 944 | return (0); |
9babb374 | 945 | } |
34dc7c2f | 946 | |
13fe0198 MA |
947 | static void |
948 | dmu_objset_clone_sync(void *arg, dmu_tx_t *tx) | |
34dc7c2f | 949 | { |
13fe0198 MA |
950 | dmu_objset_clone_arg_t *doca = arg; |
951 | dsl_pool_t *dp = dmu_tx_pool(tx); | |
952 | dsl_dir_t *pdd; | |
953 | const char *tail; | |
954 | dsl_dataset_t *origin, *ds; | |
955 | uint64_t obj; | |
956 | char namebuf[MAXNAMELEN]; | |
6f1ffb06 | 957 | |
13fe0198 MA |
958 | VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail)); |
959 | VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin)); | |
6f1ffb06 | 960 | |
13fe0198 MA |
961 | obj = dsl_dataset_create_sync(pdd, tail, origin, 0, |
962 | doca->doca_cred, tx); | |
34dc7c2f | 963 | |
13fe0198 MA |
964 | VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds)); |
965 | dsl_dataset_name(origin, namebuf); | |
966 | spa_history_log_internal_ds(ds, "clone", tx, | |
967 | "origin=%s (%llu)", namebuf, origin->ds_object); | |
a0bd735a | 968 | zvol_create_minors(dp->dp_spa, doca->doca_clone, B_TRUE); |
13fe0198 MA |
969 | dsl_dataset_rele(ds, FTAG); |
970 | dsl_dataset_rele(origin, FTAG); | |
971 | dsl_dir_rele(pdd, FTAG); | |
34dc7c2f BB |
972 | } |
973 | ||
974 | int | |
13fe0198 | 975 | dmu_objset_clone(const char *clone, const char *origin) |
34dc7c2f | 976 | { |
13fe0198 | 977 | dmu_objset_clone_arg_t doca; |
34dc7c2f | 978 | |
13fe0198 MA |
979 | doca.doca_clone = clone; |
980 | doca.doca_origin = origin; | |
981 | doca.doca_cred = CRED(); | |
572e2857 | 982 | |
13fe0198 | 983 | return (dsl_sync_task(clone, |
3d45fdd6 MA |
984 | dmu_objset_clone_check, dmu_objset_clone_sync, &doca, |
985 | 5, ZFS_SPACE_CHECK_NORMAL)); | |
6f1ffb06 MA |
986 | } |
987 | ||
988 | int | |
989 | dmu_objset_snapshot_one(const char *fsname, const char *snapname) | |
990 | { | |
991 | int err; | |
992 | char *longsnap = kmem_asprintf("%s@%s", fsname, snapname); | |
993 | nvlist_t *snaps = fnvlist_alloc(); | |
994 | ||
995 | fnvlist_add_boolean(snaps, longsnap); | |
6f1ffb06 | 996 | strfree(longsnap); |
13fe0198 MA |
997 | err = dsl_dataset_snapshot(snaps, NULL, NULL); |
998 | fnvlist_free(snaps); | |
6f1ffb06 MA |
999 | return (err); |
1000 | } | |
1001 | ||
34dc7c2f | 1002 | static void |
9babb374 | 1003 | dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx) |
34dc7c2f BB |
1004 | { |
1005 | dnode_t *dn; | |
1006 | ||
c65aa5b2 | 1007 | while ((dn = list_head(list))) { |
34dc7c2f BB |
1008 | ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); |
1009 | ASSERT(dn->dn_dbuf->db_data_pending); | |
1010 | /* | |
9babb374 BB |
1011 | * Initialize dn_zio outside dnode_sync() because the |
1012 | * meta-dnode needs to set it ouside dnode_sync(). | |
34dc7c2f BB |
1013 | */ |
1014 | dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio; | |
1015 | ASSERT(dn->dn_zio); | |
1016 | ||
1017 | ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS); | |
1018 | list_remove(list, dn); | |
9babb374 BB |
1019 | |
1020 | if (newlist) { | |
1021 | (void) dnode_add_ref(dn, newlist); | |
1022 | list_insert_tail(newlist, dn); | |
1023 | } | |
1024 | ||
34dc7c2f BB |
1025 | dnode_sync(dn, tx); |
1026 | } | |
1027 | } | |
1028 | ||
1029 | /* ARGSUSED */ | |
1030 | static void | |
428870ff | 1031 | dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg) |
34dc7c2f | 1032 | { |
d6320ddb BB |
1033 | int i; |
1034 | ||
b128c09f | 1035 | blkptr_t *bp = zio->io_bp; |
428870ff | 1036 | objset_t *os = arg; |
34dc7c2f | 1037 | dnode_phys_t *dnp = &os->os_phys->os_meta_dnode; |
34dc7c2f | 1038 | |
9b67f605 | 1039 | ASSERT(!BP_IS_EMBEDDED(bp)); |
13fe0198 MA |
1040 | ASSERT3P(bp, ==, os->os_rootbp); |
1041 | ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET); | |
1042 | ASSERT0(BP_GET_LEVEL(bp)); | |
34dc7c2f BB |
1043 | |
1044 | /* | |
9babb374 BB |
1045 | * Update rootbp fill count: it should be the number of objects |
1046 | * allocated in the object set (not counting the "special" | |
1047 | * objects that are stored in the objset_phys_t -- the meta | |
1048 | * dnode and user/group accounting objects). | |
34dc7c2f | 1049 | */ |
9babb374 | 1050 | bp->blk_fill = 0; |
d6320ddb | 1051 | for (i = 0; i < dnp->dn_nblkptr; i++) |
9b67f605 | 1052 | bp->blk_fill += BP_GET_FILL(&dnp->dn_blkptr[i]); |
428870ff BB |
1053 | } |
1054 | ||
1055 | /* ARGSUSED */ | |
1056 | static void | |
1057 | dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg) | |
1058 | { | |
1059 | blkptr_t *bp = zio->io_bp; | |
1060 | blkptr_t *bp_orig = &zio->io_bp_orig; | |
1061 | objset_t *os = arg; | |
34dc7c2f | 1062 | |
b128c09f | 1063 | if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { |
428870ff | 1064 | ASSERT(BP_EQUAL(bp, bp_orig)); |
b128c09f | 1065 | } else { |
428870ff BB |
1066 | dsl_dataset_t *ds = os->os_dsl_dataset; |
1067 | dmu_tx_t *tx = os->os_synctx; | |
1068 | ||
1069 | (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE); | |
1070 | dsl_dataset_block_born(ds, bp, tx); | |
34dc7c2f BB |
1071 | } |
1072 | } | |
1073 | ||
34dc7c2f BB |
1074 | /* called from dsl */ |
1075 | void | |
428870ff | 1076 | dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx) |
34dc7c2f BB |
1077 | { |
1078 | int txgoff; | |
5dbd68a3 | 1079 | zbookmark_phys_t zb; |
428870ff | 1080 | zio_prop_t zp; |
34dc7c2f BB |
1081 | zio_t *zio; |
1082 | list_t *list; | |
9babb374 | 1083 | list_t *newlist = NULL; |
34dc7c2f BB |
1084 | dbuf_dirty_record_t *dr; |
1085 | ||
1086 | dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg); | |
1087 | ||
1088 | ASSERT(dmu_tx_is_syncing(tx)); | |
1089 | /* XXX the write_done callback should really give us the tx... */ | |
1090 | os->os_synctx = tx; | |
1091 | ||
1092 | if (os->os_dsl_dataset == NULL) { | |
1093 | /* | |
1094 | * This is the MOS. If we have upgraded, | |
1095 | * spa_max_replication() could change, so reset | |
1096 | * os_copies here. | |
1097 | */ | |
1098 | os->os_copies = spa_max_replication(os->os_spa); | |
1099 | } | |
1100 | ||
1101 | /* | |
1102 | * Create the root block IO | |
1103 | */ | |
428870ff BB |
1104 | SET_BOOKMARK(&zb, os->os_dsl_dataset ? |
1105 | os->os_dsl_dataset->ds_object : DMU_META_OBJSET, | |
1106 | ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); | |
294f6806 | 1107 | arc_release(os->os_phys_buf, &os->os_phys_buf); |
b128c09f | 1108 | |
428870ff | 1109 | dmu_write_policy(os, NULL, 0, 0, &zp); |
9babb374 | 1110 | |
428870ff | 1111 | zio = arc_write(pio, os->os_spa, tx->tx_txg, |
3a17a7a9 | 1112 | os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os), |
bc77ba73 PD |
1113 | DMU_OS_IS_L2COMPRESSIBLE(os), |
1114 | &zp, dmu_objset_write_ready, NULL, NULL, dmu_objset_write_done, | |
1115 | os, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb); | |
34dc7c2f BB |
1116 | |
1117 | /* | |
9babb374 | 1118 | * Sync special dnodes - the parent IO for the sync is the root block |
34dc7c2f | 1119 | */ |
572e2857 BB |
1120 | DMU_META_DNODE(os)->dn_zio = zio; |
1121 | dnode_sync(DMU_META_DNODE(os), tx); | |
34dc7c2f | 1122 | |
9babb374 BB |
1123 | os->os_phys->os_flags = os->os_flags; |
1124 | ||
572e2857 BB |
1125 | if (DMU_USERUSED_DNODE(os) && |
1126 | DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) { | |
1127 | DMU_USERUSED_DNODE(os)->dn_zio = zio; | |
1128 | dnode_sync(DMU_USERUSED_DNODE(os), tx); | |
1129 | DMU_GROUPUSED_DNODE(os)->dn_zio = zio; | |
1130 | dnode_sync(DMU_GROUPUSED_DNODE(os), tx); | |
9babb374 BB |
1131 | } |
1132 | ||
34dc7c2f BB |
1133 | txgoff = tx->tx_txg & TXG_MASK; |
1134 | ||
9babb374 BB |
1135 | if (dmu_objset_userused_enabled(os)) { |
1136 | newlist = &os->os_synced_dnodes; | |
1137 | /* | |
1138 | * We must create the list here because it uses the | |
1139 | * dn_dirty_link[] of this txg. | |
1140 | */ | |
1141 | list_create(newlist, sizeof (dnode_t), | |
1142 | offsetof(dnode_t, dn_dirty_link[txgoff])); | |
1143 | } | |
1144 | ||
1145 | dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx); | |
1146 | dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx); | |
34dc7c2f | 1147 | |
572e2857 | 1148 | list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff]; |
6f1ffb06 | 1149 | while ((dr = list_head(list))) { |
13fe0198 | 1150 | ASSERT0(dr->dr_dbuf->db_level); |
34dc7c2f BB |
1151 | list_remove(list, dr); |
1152 | if (dr->dr_zio) | |
1153 | zio_nowait(dr->dr_zio); | |
1154 | } | |
1155 | /* | |
1156 | * Free intent log blocks up to this tx. | |
1157 | */ | |
1158 | zil_sync(os->os_zil, tx); | |
b128c09f | 1159 | os->os_phys->os_zil_header = os->os_zil_header; |
34dc7c2f BB |
1160 | zio_nowait(zio); |
1161 | } | |
1162 | ||
428870ff BB |
1163 | boolean_t |
1164 | dmu_objset_is_dirty(objset_t *os, uint64_t txg) | |
1165 | { | |
1166 | return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) || | |
1167 | !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK])); | |
1168 | } | |
1169 | ||
572e2857 | 1170 | static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES]; |
9babb374 BB |
1171 | |
1172 | void | |
1173 | dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb) | |
1174 | { | |
1175 | used_cbs[ost] = cb; | |
1176 | } | |
1177 | ||
1178 | boolean_t | |
428870ff | 1179 | dmu_objset_userused_enabled(objset_t *os) |
9babb374 BB |
1180 | { |
1181 | return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE && | |
572e2857 BB |
1182 | used_cbs[os->os_phys->os_type] != NULL && |
1183 | DMU_USERUSED_DNODE(os) != NULL); | |
9babb374 BB |
1184 | } |
1185 | ||
428870ff BB |
1186 | static void |
1187 | do_userquota_update(objset_t *os, uint64_t used, uint64_t flags, | |
1188 | uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx) | |
1189 | { | |
1190 | if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) { | |
1191 | int64_t delta = DNODE_SIZE + used; | |
1192 | if (subtract) | |
1193 | delta = -delta; | |
1194 | VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT, | |
1195 | user, delta, tx)); | |
1196 | VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT, | |
1197 | group, delta, tx)); | |
1198 | } | |
1199 | } | |
1200 | ||
9babb374 | 1201 | void |
428870ff | 1202 | dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx) |
9babb374 BB |
1203 | { |
1204 | dnode_t *dn; | |
1205 | list_t *list = &os->os_synced_dnodes; | |
9babb374 BB |
1206 | |
1207 | ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os)); | |
1208 | ||
6f1ffb06 | 1209 | while ((dn = list_head(list))) { |
572e2857 | 1210 | int flags; |
9babb374 | 1211 | ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object)); |
9babb374 BB |
1212 | ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE || |
1213 | dn->dn_phys->dn_flags & | |
1214 | DNODE_FLAG_USERUSED_ACCOUNTED); | |
1215 | ||
1216 | /* Allocate the user/groupused objects if necessary. */ | |
572e2857 | 1217 | if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) { |
428870ff | 1218 | VERIFY(0 == zap_create_claim(os, |
9babb374 BB |
1219 | DMU_USERUSED_OBJECT, |
1220 | DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx)); | |
428870ff | 1221 | VERIFY(0 == zap_create_claim(os, |
9babb374 BB |
1222 | DMU_GROUPUSED_OBJECT, |
1223 | DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx)); | |
1224 | } | |
1225 | ||
1226 | /* | |
428870ff BB |
1227 | * We intentionally modify the zap object even if the |
1228 | * net delta is zero. Otherwise | |
1229 | * the block of the zap obj could be shared between | |
1230 | * datasets but need to be different between them after | |
1231 | * a bprewrite. | |
9babb374 | 1232 | */ |
9babb374 | 1233 | |
572e2857 BB |
1234 | flags = dn->dn_id_flags; |
1235 | ASSERT(flags); | |
1236 | if (flags & DN_ID_OLD_EXIST) { | |
428870ff BB |
1237 | do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags, |
1238 | dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx); | |
1239 | } | |
572e2857 | 1240 | if (flags & DN_ID_NEW_EXIST) { |
428870ff BB |
1241 | do_userquota_update(os, DN_USED_BYTES(dn->dn_phys), |
1242 | dn->dn_phys->dn_flags, dn->dn_newuid, | |
1243 | dn->dn_newgid, B_FALSE, tx); | |
1244 | } | |
1245 | ||
572e2857 | 1246 | mutex_enter(&dn->dn_mtx); |
428870ff BB |
1247 | dn->dn_oldused = 0; |
1248 | dn->dn_oldflags = 0; | |
1249 | if (dn->dn_id_flags & DN_ID_NEW_EXIST) { | |
1250 | dn->dn_olduid = dn->dn_newuid; | |
1251 | dn->dn_oldgid = dn->dn_newgid; | |
1252 | dn->dn_id_flags |= DN_ID_OLD_EXIST; | |
1253 | if (dn->dn_bonuslen == 0) | |
1254 | dn->dn_id_flags |= DN_ID_CHKED_SPILL; | |
1255 | else | |
1256 | dn->dn_id_flags |= DN_ID_CHKED_BONUS; | |
1257 | } | |
1258 | dn->dn_id_flags &= ~(DN_ID_NEW_EXIST); | |
9babb374 BB |
1259 | mutex_exit(&dn->dn_mtx); |
1260 | ||
1261 | list_remove(list, dn); | |
1262 | dnode_rele(dn, list); | |
1263 | } | |
1264 | } | |
1265 | ||
428870ff BB |
1266 | /* |
1267 | * Returns a pointer to data to find uid/gid from | |
1268 | * | |
1269 | * If a dirty record for transaction group that is syncing can't | |
1270 | * be found then NULL is returned. In the NULL case it is assumed | |
1271 | * the uid/gid aren't changing. | |
1272 | */ | |
1273 | static void * | |
1274 | dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx) | |
1275 | { | |
1276 | dbuf_dirty_record_t *dr, **drp; | |
1277 | void *data; | |
1278 | ||
1279 | if (db->db_dirtycnt == 0) | |
1280 | return (db->db.db_data); /* Nothing is changing */ | |
1281 | ||
1282 | for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next) | |
1283 | if (dr->dr_txg == tx->tx_txg) | |
1284 | break; | |
1285 | ||
572e2857 | 1286 | if (dr == NULL) { |
428870ff | 1287 | data = NULL; |
572e2857 BB |
1288 | } else { |
1289 | dnode_t *dn; | |
1290 | ||
1291 | DB_DNODE_ENTER(dr->dr_dbuf); | |
1292 | dn = DB_DNODE(dr->dr_dbuf); | |
1293 | ||
1294 | if (dn->dn_bonuslen == 0 && | |
1295 | dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID) | |
1296 | data = dr->dt.dl.dr_data->b_data; | |
1297 | else | |
1298 | data = dr->dt.dl.dr_data; | |
1299 | ||
1300 | DB_DNODE_EXIT(dr->dr_dbuf); | |
1301 | } | |
1302 | ||
428870ff BB |
1303 | return (data); |
1304 | } | |
1305 | ||
1306 | void | |
1307 | dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx) | |
1308 | { | |
1309 | objset_t *os = dn->dn_objset; | |
1310 | void *data = NULL; | |
1311 | dmu_buf_impl_t *db = NULL; | |
a117a6d6 GW |
1312 | uint64_t *user = NULL; |
1313 | uint64_t *group = NULL; | |
428870ff BB |
1314 | int flags = dn->dn_id_flags; |
1315 | int error; | |
1316 | boolean_t have_spill = B_FALSE; | |
1317 | ||
1318 | if (!dmu_objset_userused_enabled(dn->dn_objset)) | |
1319 | return; | |
1320 | ||
1321 | if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST| | |
1322 | DN_ID_CHKED_SPILL))) | |
1323 | return; | |
1324 | ||
1325 | if (before && dn->dn_bonuslen != 0) | |
1326 | data = DN_BONUS(dn->dn_phys); | |
1327 | else if (!before && dn->dn_bonuslen != 0) { | |
a3000f93 BB |
1328 | if (dn->dn_bonus) { |
1329 | db = dn->dn_bonus; | |
428870ff BB |
1330 | mutex_enter(&db->db_mtx); |
1331 | data = dmu_objset_userquota_find_data(db, tx); | |
1332 | } else { | |
1333 | data = DN_BONUS(dn->dn_phys); | |
1334 | } | |
1335 | } else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) { | |
1336 | int rf = 0; | |
1337 | ||
1338 | if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) | |
1339 | rf |= DB_RF_HAVESTRUCT; | |
572e2857 BB |
1340 | error = dmu_spill_hold_by_dnode(dn, |
1341 | rf | DB_RF_MUST_SUCCEED, | |
428870ff BB |
1342 | FTAG, (dmu_buf_t **)&db); |
1343 | ASSERT(error == 0); | |
1344 | mutex_enter(&db->db_mtx); | |
1345 | data = (before) ? db->db.db_data : | |
1346 | dmu_objset_userquota_find_data(db, tx); | |
1347 | have_spill = B_TRUE; | |
1348 | } else { | |
1349 | mutex_enter(&dn->dn_mtx); | |
1350 | dn->dn_id_flags |= DN_ID_CHKED_BONUS; | |
1351 | mutex_exit(&dn->dn_mtx); | |
1352 | return; | |
1353 | } | |
1354 | ||
1355 | if (before) { | |
1356 | ASSERT(data); | |
1357 | user = &dn->dn_olduid; | |
1358 | group = &dn->dn_oldgid; | |
1359 | } else if (data) { | |
1360 | user = &dn->dn_newuid; | |
1361 | group = &dn->dn_newgid; | |
1362 | } | |
1363 | ||
1364 | /* | |
1365 | * Must always call the callback in case the object | |
1366 | * type has changed and that type isn't an object type to track | |
1367 | */ | |
1368 | error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data, | |
1369 | user, group); | |
1370 | ||
1371 | /* | |
1372 | * Preserve existing uid/gid when the callback can't determine | |
1373 | * what the new uid/gid are and the callback returned EEXIST. | |
1374 | * The EEXIST error tells us to just use the existing uid/gid. | |
1375 | * If we don't know what the old values are then just assign | |
1376 | * them to 0, since that is a new file being created. | |
1377 | */ | |
1378 | if (!before && data == NULL && error == EEXIST) { | |
1379 | if (flags & DN_ID_OLD_EXIST) { | |
1380 | dn->dn_newuid = dn->dn_olduid; | |
1381 | dn->dn_newgid = dn->dn_oldgid; | |
1382 | } else { | |
1383 | dn->dn_newuid = 0; | |
1384 | dn->dn_newgid = 0; | |
1385 | } | |
1386 | error = 0; | |
1387 | } | |
1388 | ||
1389 | if (db) | |
1390 | mutex_exit(&db->db_mtx); | |
1391 | ||
1392 | mutex_enter(&dn->dn_mtx); | |
1393 | if (error == 0 && before) | |
1394 | dn->dn_id_flags |= DN_ID_OLD_EXIST; | |
1395 | if (error == 0 && !before) | |
1396 | dn->dn_id_flags |= DN_ID_NEW_EXIST; | |
1397 | ||
1398 | if (have_spill) { | |
1399 | dn->dn_id_flags |= DN_ID_CHKED_SPILL; | |
1400 | } else { | |
1401 | dn->dn_id_flags |= DN_ID_CHKED_BONUS; | |
1402 | } | |
1403 | mutex_exit(&dn->dn_mtx); | |
a3000f93 | 1404 | if (have_spill) |
428870ff BB |
1405 | dmu_buf_rele((dmu_buf_t *)db, FTAG); |
1406 | } | |
1407 | ||
9babb374 BB |
1408 | boolean_t |
1409 | dmu_objset_userspace_present(objset_t *os) | |
1410 | { | |
428870ff | 1411 | return (os->os_phys->os_flags & |
9babb374 BB |
1412 | OBJSET_FLAG_USERACCOUNTING_COMPLETE); |
1413 | } | |
1414 | ||
1415 | int | |
1416 | dmu_objset_userspace_upgrade(objset_t *os) | |
1417 | { | |
1418 | uint64_t obj; | |
1419 | int err = 0; | |
1420 | ||
1421 | if (dmu_objset_userspace_present(os)) | |
1422 | return (0); | |
428870ff | 1423 | if (!dmu_objset_userused_enabled(os)) |
2e528b49 | 1424 | return (SET_ERROR(ENOTSUP)); |
9babb374 | 1425 | if (dmu_objset_is_snapshot(os)) |
2e528b49 | 1426 | return (SET_ERROR(EINVAL)); |
9babb374 BB |
1427 | |
1428 | /* | |
1429 | * We simply need to mark every object dirty, so that it will be | |
1430 | * synced out and now accounted. If this is called | |
1431 | * concurrently, or if we already did some work before crashing, | |
1432 | * that's fine, since we track each object's accounted state | |
1433 | * independently. | |
1434 | */ | |
1435 | ||
1436 | for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) { | |
45d1cae3 | 1437 | dmu_tx_t *tx; |
9babb374 BB |
1438 | dmu_buf_t *db; |
1439 | int objerr; | |
1440 | ||
1441 | if (issig(JUSTLOOKING) && issig(FORREAL)) | |
2e528b49 | 1442 | return (SET_ERROR(EINTR)); |
9babb374 BB |
1443 | |
1444 | objerr = dmu_bonus_hold(os, obj, FTAG, &db); | |
13fe0198 | 1445 | if (objerr != 0) |
9babb374 | 1446 | continue; |
45d1cae3 | 1447 | tx = dmu_tx_create(os); |
9babb374 BB |
1448 | dmu_tx_hold_bonus(tx, obj); |
1449 | objerr = dmu_tx_assign(tx, TXG_WAIT); | |
13fe0198 | 1450 | if (objerr != 0) { |
9babb374 BB |
1451 | dmu_tx_abort(tx); |
1452 | continue; | |
1453 | } | |
1454 | dmu_buf_will_dirty(db, tx); | |
1455 | dmu_buf_rele(db, FTAG); | |
1456 | dmu_tx_commit(tx); | |
1457 | } | |
1458 | ||
428870ff | 1459 | os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE; |
9babb374 BB |
1460 | txg_wait_synced(dmu_objset_pool(os), 0); |
1461 | return (0); | |
1462 | } | |
1463 | ||
34dc7c2f BB |
1464 | void |
1465 | dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp, | |
1466 | uint64_t *usedobjsp, uint64_t *availobjsp) | |
1467 | { | |
428870ff | 1468 | dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp, |
34dc7c2f BB |
1469 | usedobjsp, availobjsp); |
1470 | } | |
1471 | ||
1472 | uint64_t | |
1473 | dmu_objset_fsid_guid(objset_t *os) | |
1474 | { | |
428870ff | 1475 | return (dsl_dataset_fsid_guid(os->os_dsl_dataset)); |
34dc7c2f BB |
1476 | } |
1477 | ||
1478 | void | |
1479 | dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat) | |
1480 | { | |
428870ff BB |
1481 | stat->dds_type = os->os_phys->os_type; |
1482 | if (os->os_dsl_dataset) | |
1483 | dsl_dataset_fast_stat(os->os_dsl_dataset, stat); | |
34dc7c2f BB |
1484 | } |
1485 | ||
1486 | void | |
1487 | dmu_objset_stats(objset_t *os, nvlist_t *nv) | |
1488 | { | |
428870ff BB |
1489 | ASSERT(os->os_dsl_dataset || |
1490 | os->os_phys->os_type == DMU_OST_META); | |
34dc7c2f | 1491 | |
428870ff BB |
1492 | if (os->os_dsl_dataset != NULL) |
1493 | dsl_dataset_stats(os->os_dsl_dataset, nv); | |
34dc7c2f BB |
1494 | |
1495 | dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE, | |
428870ff | 1496 | os->os_phys->os_type); |
9babb374 BB |
1497 | dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING, |
1498 | dmu_objset_userspace_present(os)); | |
34dc7c2f BB |
1499 | } |
1500 | ||
1501 | int | |
1502 | dmu_objset_is_snapshot(objset_t *os) | |
1503 | { | |
428870ff | 1504 | if (os->os_dsl_dataset != NULL) |
0c66c32d | 1505 | return (os->os_dsl_dataset->ds_is_snapshot); |
34dc7c2f BB |
1506 | else |
1507 | return (B_FALSE); | |
1508 | } | |
1509 | ||
1510 | int | |
1511 | dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen, | |
1512 | boolean_t *conflict) | |
1513 | { | |
428870ff | 1514 | dsl_dataset_t *ds = os->os_dsl_dataset; |
34dc7c2f BB |
1515 | uint64_t ignored; |
1516 | ||
d683ddbb | 1517 | if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0) |
2e528b49 | 1518 | return (SET_ERROR(ENOENT)); |
34dc7c2f BB |
1519 | |
1520 | return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset, | |
d683ddbb JG |
1521 | dsl_dataset_phys(ds)->ds_snapnames_zapobj, name, 8, 1, &ignored, |
1522 | MT_FIRST, real, maxlen, conflict)); | |
34dc7c2f BB |
1523 | } |
1524 | ||
1525 | int | |
1526 | dmu_snapshot_list_next(objset_t *os, int namelen, char *name, | |
1527 | uint64_t *idp, uint64_t *offp, boolean_t *case_conflict) | |
1528 | { | |
428870ff | 1529 | dsl_dataset_t *ds = os->os_dsl_dataset; |
34dc7c2f BB |
1530 | zap_cursor_t cursor; |
1531 | zap_attribute_t attr; | |
1532 | ||
13fe0198 MA |
1533 | ASSERT(dsl_pool_config_held(dmu_objset_pool(os))); |
1534 | ||
d683ddbb | 1535 | if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0) |
2e528b49 | 1536 | return (SET_ERROR(ENOENT)); |
34dc7c2f BB |
1537 | |
1538 | zap_cursor_init_serialized(&cursor, | |
1539 | ds->ds_dir->dd_pool->dp_meta_objset, | |
d683ddbb | 1540 | dsl_dataset_phys(ds)->ds_snapnames_zapobj, *offp); |
34dc7c2f BB |
1541 | |
1542 | if (zap_cursor_retrieve(&cursor, &attr) != 0) { | |
1543 | zap_cursor_fini(&cursor); | |
2e528b49 | 1544 | return (SET_ERROR(ENOENT)); |
34dc7c2f BB |
1545 | } |
1546 | ||
1547 | if (strlen(attr.za_name) + 1 > namelen) { | |
1548 | zap_cursor_fini(&cursor); | |
2e528b49 | 1549 | return (SET_ERROR(ENAMETOOLONG)); |
34dc7c2f BB |
1550 | } |
1551 | ||
1552 | (void) strcpy(name, attr.za_name); | |
1553 | if (idp) | |
1554 | *idp = attr.za_first_integer; | |
1555 | if (case_conflict) | |
1556 | *case_conflict = attr.za_normalization_conflict; | |
1557 | zap_cursor_advance(&cursor); | |
1558 | *offp = zap_cursor_serialize(&cursor); | |
1559 | zap_cursor_fini(&cursor); | |
1560 | ||
1561 | return (0); | |
1562 | } | |
1563 | ||
ebe7e575 | 1564 | int |
6772fb67 | 1565 | dmu_snapshot_lookup(objset_t *os, const char *name, uint64_t *value) |
ebe7e575 | 1566 | { |
d1d7e268 | 1567 | return (dsl_dataset_snap_lookup(os->os_dsl_dataset, name, value)); |
ebe7e575 BB |
1568 | } |
1569 | ||
34dc7c2f BB |
1570 | int |
1571 | dmu_dir_list_next(objset_t *os, int namelen, char *name, | |
1572 | uint64_t *idp, uint64_t *offp) | |
1573 | { | |
428870ff | 1574 | dsl_dir_t *dd = os->os_dsl_dataset->ds_dir; |
34dc7c2f BB |
1575 | zap_cursor_t cursor; |
1576 | zap_attribute_t attr; | |
1577 | ||
1578 | /* there is no next dir on a snapshot! */ | |
428870ff | 1579 | if (os->os_dsl_dataset->ds_object != |
d683ddbb | 1580 | dsl_dir_phys(dd)->dd_head_dataset_obj) |
2e528b49 | 1581 | return (SET_ERROR(ENOENT)); |
34dc7c2f BB |
1582 | |
1583 | zap_cursor_init_serialized(&cursor, | |
1584 | dd->dd_pool->dp_meta_objset, | |
d683ddbb | 1585 | dsl_dir_phys(dd)->dd_child_dir_zapobj, *offp); |
34dc7c2f BB |
1586 | |
1587 | if (zap_cursor_retrieve(&cursor, &attr) != 0) { | |
1588 | zap_cursor_fini(&cursor); | |
2e528b49 | 1589 | return (SET_ERROR(ENOENT)); |
34dc7c2f BB |
1590 | } |
1591 | ||
1592 | if (strlen(attr.za_name) + 1 > namelen) { | |
1593 | zap_cursor_fini(&cursor); | |
2e528b49 | 1594 | return (SET_ERROR(ENAMETOOLONG)); |
34dc7c2f BB |
1595 | } |
1596 | ||
1597 | (void) strcpy(name, attr.za_name); | |
1598 | if (idp) | |
1599 | *idp = attr.za_first_integer; | |
1600 | zap_cursor_advance(&cursor); | |
1601 | *offp = zap_cursor_serialize(&cursor); | |
1602 | zap_cursor_fini(&cursor); | |
1603 | ||
1604 | return (0); | |
1605 | } | |
1606 | ||
9c43027b AJ |
1607 | typedef struct dmu_objset_find_ctx { |
1608 | taskq_t *dc_tq; | |
1609 | dsl_pool_t *dc_dp; | |
1610 | uint64_t dc_ddobj; | |
1611 | int (*dc_func)(dsl_pool_t *, dsl_dataset_t *, void *); | |
1612 | void *dc_arg; | |
1613 | int dc_flags; | |
1614 | kmutex_t *dc_error_lock; | |
1615 | int *dc_error; | |
1616 | } dmu_objset_find_ctx_t; | |
1617 | ||
1618 | static void | |
1619 | dmu_objset_find_dp_impl(dmu_objset_find_ctx_t *dcp) | |
b128c09f | 1620 | { |
9c43027b AJ |
1621 | dsl_pool_t *dp = dcp->dc_dp; |
1622 | dmu_objset_find_ctx_t *child_dcp; | |
13fe0198 MA |
1623 | dsl_dir_t *dd; |
1624 | dsl_dataset_t *ds; | |
1625 | zap_cursor_t zc; | |
1626 | zap_attribute_t *attr; | |
1627 | uint64_t thisobj; | |
9c43027b | 1628 | int err = 0; |
13fe0198 | 1629 | |
9c43027b AJ |
1630 | /* don't process if there already was an error */ |
1631 | if (*dcp->dc_error != 0) | |
1632 | goto out; | |
13fe0198 | 1633 | |
9c43027b | 1634 | err = dsl_dir_hold_obj(dp, dcp->dc_ddobj, NULL, FTAG, &dd); |
13fe0198 | 1635 | if (err != 0) |
9c43027b | 1636 | goto out; |
13fe0198 MA |
1637 | |
1638 | /* Don't visit hidden ($MOS & $ORIGIN) objsets. */ | |
1639 | if (dd->dd_myname[0] == '$') { | |
1640 | dsl_dir_rele(dd, FTAG); | |
9c43027b | 1641 | goto out; |
13fe0198 MA |
1642 | } |
1643 | ||
d683ddbb | 1644 | thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj; |
79c76d5b | 1645 | attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); |
13fe0198 MA |
1646 | |
1647 | /* | |
1648 | * Iterate over all children. | |
1649 | */ | |
9c43027b | 1650 | if (dcp->dc_flags & DS_FIND_CHILDREN) { |
13fe0198 | 1651 | for (zap_cursor_init(&zc, dp->dp_meta_objset, |
d683ddbb | 1652 | dsl_dir_phys(dd)->dd_child_dir_zapobj); |
13fe0198 MA |
1653 | zap_cursor_retrieve(&zc, attr) == 0; |
1654 | (void) zap_cursor_advance(&zc)) { | |
1655 | ASSERT3U(attr->za_integer_length, ==, | |
1656 | sizeof (uint64_t)); | |
1657 | ASSERT3U(attr->za_num_integers, ==, 1); | |
1658 | ||
9c43027b AJ |
1659 | child_dcp = kmem_alloc(sizeof (*child_dcp), KM_SLEEP); |
1660 | *child_dcp = *dcp; | |
1661 | child_dcp->dc_ddobj = attr->za_first_integer; | |
1662 | if (dcp->dc_tq != NULL) | |
1663 | (void) taskq_dispatch(dcp->dc_tq, | |
1664 | dmu_objset_find_dp_cb, child_dcp, TQ_SLEEP); | |
1665 | else | |
1666 | dmu_objset_find_dp_impl(child_dcp); | |
13fe0198 MA |
1667 | } |
1668 | zap_cursor_fini(&zc); | |
13fe0198 MA |
1669 | } |
1670 | ||
1671 | /* | |
1672 | * Iterate over all snapshots. | |
1673 | */ | |
9c43027b | 1674 | if (dcp->dc_flags & DS_FIND_SNAPSHOTS) { |
13fe0198 MA |
1675 | dsl_dataset_t *ds; |
1676 | err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds); | |
1677 | ||
1678 | if (err == 0) { | |
d683ddbb JG |
1679 | uint64_t snapobj; |
1680 | ||
1681 | snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj; | |
13fe0198 MA |
1682 | dsl_dataset_rele(ds, FTAG); |
1683 | ||
1684 | for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj); | |
1685 | zap_cursor_retrieve(&zc, attr) == 0; | |
1686 | (void) zap_cursor_advance(&zc)) { | |
1687 | ASSERT3U(attr->za_integer_length, ==, | |
1688 | sizeof (uint64_t)); | |
1689 | ASSERT3U(attr->za_num_integers, ==, 1); | |
1690 | ||
1691 | err = dsl_dataset_hold_obj(dp, | |
1692 | attr->za_first_integer, FTAG, &ds); | |
1693 | if (err != 0) | |
1694 | break; | |
9c43027b | 1695 | err = dcp->dc_func(dp, ds, dcp->dc_arg); |
13fe0198 MA |
1696 | dsl_dataset_rele(ds, FTAG); |
1697 | if (err != 0) | |
1698 | break; | |
1699 | } | |
1700 | zap_cursor_fini(&zc); | |
1701 | } | |
1702 | } | |
1703 | ||
1704 | dsl_dir_rele(dd, FTAG); | |
1705 | kmem_free(attr, sizeof (zap_attribute_t)); | |
1706 | ||
1707 | if (err != 0) | |
9c43027b | 1708 | goto out; |
13fe0198 MA |
1709 | |
1710 | /* | |
1711 | * Apply to self. | |
1712 | */ | |
1713 | err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds); | |
1714 | if (err != 0) | |
9c43027b AJ |
1715 | goto out; |
1716 | err = dcp->dc_func(dp, ds, dcp->dc_arg); | |
13fe0198 | 1717 | dsl_dataset_rele(ds, FTAG); |
9c43027b AJ |
1718 | |
1719 | out: | |
1720 | if (err != 0) { | |
1721 | mutex_enter(dcp->dc_error_lock); | |
1722 | /* only keep first error */ | |
1723 | if (*dcp->dc_error == 0) | |
1724 | *dcp->dc_error = err; | |
1725 | mutex_exit(dcp->dc_error_lock); | |
1726 | } | |
1727 | ||
1728 | kmem_free(dcp, sizeof (*dcp)); | |
1729 | } | |
1730 | ||
1731 | static void | |
1732 | dmu_objset_find_dp_cb(void *arg) | |
1733 | { | |
1734 | dmu_objset_find_ctx_t *dcp = arg; | |
1735 | dsl_pool_t *dp = dcp->dc_dp; | |
1736 | ||
5e8cd5d1 AJ |
1737 | /* |
1738 | * We need to get a pool_config_lock here, as there are several | |
1739 | * asssert(pool_config_held) down the stack. Getting a lock via | |
1740 | * dsl_pool_config_enter is risky, as it might be stalled by a | |
1741 | * pending writer. This would deadlock, as the write lock can | |
1742 | * only be granted when our parent thread gives up the lock. | |
1743 | * The _prio interface gives us priority over a pending writer. | |
1744 | */ | |
1745 | dsl_pool_config_enter_prio(dp, FTAG); | |
9c43027b AJ |
1746 | |
1747 | dmu_objset_find_dp_impl(dcp); | |
1748 | ||
1749 | dsl_pool_config_exit(dp, FTAG); | |
1750 | } | |
1751 | ||
1752 | /* | |
1753 | * Find objsets under and including ddobj, call func(ds) on each. | |
1754 | * The order for the enumeration is completely undefined. | |
1755 | * func is called with dsl_pool_config held. | |
1756 | */ | |
1757 | int | |
1758 | dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj, | |
1759 | int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags) | |
1760 | { | |
1761 | int error = 0; | |
1762 | taskq_t *tq = NULL; | |
1763 | int ntasks; | |
1764 | dmu_objset_find_ctx_t *dcp; | |
1765 | kmutex_t err_lock; | |
1766 | ||
1767 | mutex_init(&err_lock, NULL, MUTEX_DEFAULT, NULL); | |
1768 | dcp = kmem_alloc(sizeof (*dcp), KM_SLEEP); | |
1769 | dcp->dc_tq = NULL; | |
1770 | dcp->dc_dp = dp; | |
1771 | dcp->dc_ddobj = ddobj; | |
1772 | dcp->dc_func = func; | |
1773 | dcp->dc_arg = arg; | |
1774 | dcp->dc_flags = flags; | |
1775 | dcp->dc_error_lock = &err_lock; | |
1776 | dcp->dc_error = &error; | |
1777 | ||
1778 | if ((flags & DS_FIND_SERIALIZE) || dsl_pool_config_held_writer(dp)) { | |
1779 | /* | |
1780 | * In case a write lock is held we can't make use of | |
1781 | * parallelism, as down the stack of the worker threads | |
1782 | * the lock is asserted via dsl_pool_config_held. | |
1783 | * In case of a read lock this is solved by getting a read | |
1784 | * lock in each worker thread, which isn't possible in case | |
1785 | * of a writer lock. So we fall back to the synchronous path | |
1786 | * here. | |
1787 | * In the future it might be possible to get some magic into | |
1788 | * dsl_pool_config_held in a way that it returns true for | |
1789 | * the worker threads so that a single lock held from this | |
1790 | * thread suffices. For now, stay single threaded. | |
1791 | */ | |
1792 | dmu_objset_find_dp_impl(dcp); | |
e5676636 | 1793 | mutex_destroy(&err_lock); |
9c43027b AJ |
1794 | |
1795 | return (error); | |
1796 | } | |
1797 | ||
1798 | ntasks = dmu_find_threads; | |
1799 | if (ntasks == 0) | |
1800 | ntasks = vdev_count_leaves(dp->dp_spa) * 4; | |
1229323d | 1801 | tq = taskq_create("dmu_objset_find", ntasks, maxclsyspri, ntasks, |
9c43027b AJ |
1802 | INT_MAX, 0); |
1803 | if (tq == NULL) { | |
1804 | kmem_free(dcp, sizeof (*dcp)); | |
e5676636 BB |
1805 | mutex_destroy(&err_lock); |
1806 | ||
9c43027b AJ |
1807 | return (SET_ERROR(ENOMEM)); |
1808 | } | |
1809 | dcp->dc_tq = tq; | |
1810 | ||
1811 | /* dcp will be freed by task */ | |
1812 | (void) taskq_dispatch(tq, dmu_objset_find_dp_cb, dcp, TQ_SLEEP); | |
1813 | ||
1814 | /* | |
1815 | * PORTING: this code relies on the property of taskq_wait to wait | |
1816 | * until no more tasks are queued and no more tasks are active. As | |
1817 | * we always queue new tasks from within other tasks, task_wait | |
1818 | * reliably waits for the full recursion to finish, even though we | |
1819 | * enqueue new tasks after taskq_wait has been called. | |
1820 | * On platforms other than illumos, taskq_wait may not have this | |
1821 | * property. | |
1822 | */ | |
1823 | taskq_wait(tq); | |
1824 | taskq_destroy(tq); | |
1825 | mutex_destroy(&err_lock); | |
1826 | ||
1827 | return (error); | |
b128c09f BB |
1828 | } |
1829 | ||
1830 | /* | |
13fe0198 MA |
1831 | * Find all objsets under name, and for each, call 'func(child_name, arg)'. |
1832 | * The dp_config_rwlock must not be held when this is called, and it | |
1833 | * will not be held when the callback is called. | |
1834 | * Therefore this function should only be used when the pool is not changing | |
1835 | * (e.g. in syncing context), or the callback can deal with the possible races. | |
b128c09f | 1836 | */ |
13fe0198 MA |
1837 | static int |
1838 | dmu_objset_find_impl(spa_t *spa, const char *name, | |
1839 | int func(const char *, void *), void *arg, int flags) | |
34dc7c2f BB |
1840 | { |
1841 | dsl_dir_t *dd; | |
13fe0198 | 1842 | dsl_pool_t *dp = spa_get_dsl(spa); |
b128c09f | 1843 | dsl_dataset_t *ds; |
34dc7c2f BB |
1844 | zap_cursor_t zc; |
1845 | zap_attribute_t *attr; | |
1846 | char *child; | |
b128c09f BB |
1847 | uint64_t thisobj; |
1848 | int err; | |
34dc7c2f | 1849 | |
13fe0198 MA |
1850 | dsl_pool_config_enter(dp, FTAG); |
1851 | ||
1852 | err = dsl_dir_hold(dp, name, FTAG, &dd, NULL); | |
1853 | if (err != 0) { | |
1854 | dsl_pool_config_exit(dp, FTAG); | |
34dc7c2f | 1855 | return (err); |
13fe0198 | 1856 | } |
34dc7c2f | 1857 | |
b128c09f BB |
1858 | /* Don't visit hidden ($MOS & $ORIGIN) objsets. */ |
1859 | if (dd->dd_myname[0] == '$') { | |
13fe0198 MA |
1860 | dsl_dir_rele(dd, FTAG); |
1861 | dsl_pool_config_exit(dp, FTAG); | |
b128c09f BB |
1862 | return (0); |
1863 | } | |
1864 | ||
d683ddbb | 1865 | thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj; |
79c76d5b | 1866 | attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); |
34dc7c2f BB |
1867 | |
1868 | /* | |
1869 | * Iterate over all children. | |
1870 | */ | |
1871 | if (flags & DS_FIND_CHILDREN) { | |
b128c09f | 1872 | for (zap_cursor_init(&zc, dp->dp_meta_objset, |
d683ddbb | 1873 | dsl_dir_phys(dd)->dd_child_dir_zapobj); |
34dc7c2f BB |
1874 | zap_cursor_retrieve(&zc, attr) == 0; |
1875 | (void) zap_cursor_advance(&zc)) { | |
13fe0198 MA |
1876 | ASSERT3U(attr->za_integer_length, ==, |
1877 | sizeof (uint64_t)); | |
1878 | ASSERT3U(attr->za_num_integers, ==, 1); | |
34dc7c2f | 1879 | |
428870ff | 1880 | child = kmem_asprintf("%s/%s", name, attr->za_name); |
13fe0198 MA |
1881 | dsl_pool_config_exit(dp, FTAG); |
1882 | err = dmu_objset_find_impl(spa, child, | |
1883 | func, arg, flags); | |
1884 | dsl_pool_config_enter(dp, FTAG); | |
428870ff | 1885 | strfree(child); |
13fe0198 | 1886 | if (err != 0) |
34dc7c2f BB |
1887 | break; |
1888 | } | |
1889 | zap_cursor_fini(&zc); | |
1890 | ||
13fe0198 MA |
1891 | if (err != 0) { |
1892 | dsl_dir_rele(dd, FTAG); | |
1893 | dsl_pool_config_exit(dp, FTAG); | |
34dc7c2f BB |
1894 | kmem_free(attr, sizeof (zap_attribute_t)); |
1895 | return (err); | |
1896 | } | |
1897 | } | |
1898 | ||
1899 | /* | |
1900 | * Iterate over all snapshots. | |
1901 | */ | |
b128c09f | 1902 | if (flags & DS_FIND_SNAPSHOTS) { |
b128c09f | 1903 | err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds); |
b128c09f BB |
1904 | |
1905 | if (err == 0) { | |
d683ddbb JG |
1906 | uint64_t snapobj; |
1907 | ||
1908 | snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj; | |
b128c09f BB |
1909 | dsl_dataset_rele(ds, FTAG); |
1910 | ||
1911 | for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj); | |
1912 | zap_cursor_retrieve(&zc, attr) == 0; | |
1913 | (void) zap_cursor_advance(&zc)) { | |
13fe0198 | 1914 | ASSERT3U(attr->za_integer_length, ==, |
b128c09f | 1915 | sizeof (uint64_t)); |
13fe0198 | 1916 | ASSERT3U(attr->za_num_integers, ==, 1); |
b128c09f | 1917 | |
428870ff BB |
1918 | child = kmem_asprintf("%s@%s", |
1919 | name, attr->za_name); | |
13fe0198 MA |
1920 | dsl_pool_config_exit(dp, FTAG); |
1921 | err = func(child, arg); | |
1922 | dsl_pool_config_enter(dp, FTAG); | |
428870ff | 1923 | strfree(child); |
13fe0198 | 1924 | if (err != 0) |
b128c09f BB |
1925 | break; |
1926 | } | |
1927 | zap_cursor_fini(&zc); | |
34dc7c2f | 1928 | } |
34dc7c2f BB |
1929 | } |
1930 | ||
13fe0198 | 1931 | dsl_dir_rele(dd, FTAG); |
34dc7c2f | 1932 | kmem_free(attr, sizeof (zap_attribute_t)); |
13fe0198 | 1933 | dsl_pool_config_exit(dp, FTAG); |
34dc7c2f | 1934 | |
13fe0198 | 1935 | if (err != 0) |
34dc7c2f BB |
1936 | return (err); |
1937 | ||
13fe0198 MA |
1938 | /* Apply to self. */ |
1939 | return (func(name, arg)); | |
34dc7c2f BB |
1940 | } |
1941 | ||
13fe0198 MA |
1942 | /* |
1943 | * See comment above dmu_objset_find_impl(). | |
1944 | */ | |
d164b209 | 1945 | int |
13fe0198 MA |
1946 | dmu_objset_find(char *name, int func(const char *, void *), void *arg, |
1947 | int flags) | |
d164b209 | 1948 | { |
13fe0198 MA |
1949 | spa_t *spa; |
1950 | int error; | |
d164b209 | 1951 | |
13fe0198 MA |
1952 | error = spa_open(name, &spa, FTAG); |
1953 | if (error != 0) | |
1954 | return (error); | |
1955 | error = dmu_objset_find_impl(spa, name, func, arg, flags); | |
1956 | spa_close(spa, FTAG); | |
1957 | return (error); | |
d164b209 BB |
1958 | } |
1959 | ||
34dc7c2f BB |
1960 | void |
1961 | dmu_objset_set_user(objset_t *os, void *user_ptr) | |
1962 | { | |
428870ff BB |
1963 | ASSERT(MUTEX_HELD(&os->os_user_ptr_lock)); |
1964 | os->os_user_ptr = user_ptr; | |
34dc7c2f BB |
1965 | } |
1966 | ||
1967 | void * | |
1968 | dmu_objset_get_user(objset_t *os) | |
1969 | { | |
428870ff BB |
1970 | ASSERT(MUTEX_HELD(&os->os_user_ptr_lock)); |
1971 | return (os->os_user_ptr); | |
34dc7c2f | 1972 | } |
c28b2279 | 1973 | |
13fe0198 MA |
1974 | /* |
1975 | * Determine name of filesystem, given name of snapshot. | |
1976 | * buf must be at least MAXNAMELEN bytes | |
1977 | */ | |
1978 | int | |
1979 | dmu_fsname(const char *snapname, char *buf) | |
1980 | { | |
1981 | char *atp = strchr(snapname, '@'); | |
1982 | if (atp == NULL) | |
2e528b49 | 1983 | return (SET_ERROR(EINVAL)); |
13fe0198 | 1984 | if (atp - snapname >= MAXNAMELEN) |
2e528b49 | 1985 | return (SET_ERROR(ENAMETOOLONG)); |
13fe0198 MA |
1986 | (void) strlcpy(buf, snapname, atp - snapname + 1); |
1987 | return (0); | |
1988 | } | |
1989 | ||
c28b2279 | 1990 | #if defined(_KERNEL) && defined(HAVE_SPL) |
f0fd83be | 1991 | EXPORT_SYMBOL(dmu_objset_zil); |
c28b2279 | 1992 | EXPORT_SYMBOL(dmu_objset_pool); |
f0fd83be BB |
1993 | EXPORT_SYMBOL(dmu_objset_ds); |
1994 | EXPORT_SYMBOL(dmu_objset_type); | |
c28b2279 BB |
1995 | EXPORT_SYMBOL(dmu_objset_name); |
1996 | EXPORT_SYMBOL(dmu_objset_hold); | |
1997 | EXPORT_SYMBOL(dmu_objset_own); | |
1998 | EXPORT_SYMBOL(dmu_objset_rele); | |
1999 | EXPORT_SYMBOL(dmu_objset_disown); | |
2000 | EXPORT_SYMBOL(dmu_objset_from_ds); | |
2001 | EXPORT_SYMBOL(dmu_objset_create); | |
2002 | EXPORT_SYMBOL(dmu_objset_clone); | |
c28b2279 BB |
2003 | EXPORT_SYMBOL(dmu_objset_stats); |
2004 | EXPORT_SYMBOL(dmu_objset_fast_stat); | |
54a179e7 | 2005 | EXPORT_SYMBOL(dmu_objset_spa); |
c28b2279 BB |
2006 | EXPORT_SYMBOL(dmu_objset_space); |
2007 | EXPORT_SYMBOL(dmu_objset_fsid_guid); | |
2008 | EXPORT_SYMBOL(dmu_objset_find); | |
c28b2279 BB |
2009 | EXPORT_SYMBOL(dmu_objset_byteswap); |
2010 | EXPORT_SYMBOL(dmu_objset_evict_dbufs); | |
2011 | EXPORT_SYMBOL(dmu_objset_snap_cmtime); | |
2012 | ||
2013 | EXPORT_SYMBOL(dmu_objset_sync); | |
2014 | EXPORT_SYMBOL(dmu_objset_is_dirty); | |
2015 | EXPORT_SYMBOL(dmu_objset_create_impl); | |
2016 | EXPORT_SYMBOL(dmu_objset_open_impl); | |
2017 | EXPORT_SYMBOL(dmu_objset_evict); | |
2018 | EXPORT_SYMBOL(dmu_objset_register_type); | |
2019 | EXPORT_SYMBOL(dmu_objset_do_userquota_updates); | |
2020 | EXPORT_SYMBOL(dmu_objset_userquota_get_ids); | |
2021 | EXPORT_SYMBOL(dmu_objset_userused_enabled); | |
2022 | EXPORT_SYMBOL(dmu_objset_userspace_upgrade); | |
2023 | EXPORT_SYMBOL(dmu_objset_userspace_present); | |
2024 | #endif |