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