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