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