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