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