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