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