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