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