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