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