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