]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zvol.c
06bc75c634a68bbdc58d26047ce771ce807fb41d
[mirror_zfs.git] / module / zfs / zvol.c
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 https://opensource.org/licenses/CDDL-1.0.
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) 2008-2010 Lawrence Livermore National Security, LLC.
23 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
24 * Rewritten for Linux by Brian Behlendorf <behlendorf1@llnl.gov>.
25 * LLNL-CODE-403049.
26 *
27 * ZFS volume emulation driver.
28 *
29 * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes.
30 * Volumes are accessed through the symbolic links named:
31 *
32 * /dev/<pool_name>/<dataset_name>
33 *
34 * Volumes are persistent through reboot and module load. No user command
35 * needs to be run before opening and using a device.
36 *
37 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
38 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
39 * Copyright (c) 2012, 2019 by Delphix. All rights reserved.
40 */
41
42 /*
43 * Note on locking of zvol state structures.
44 *
45 * These structures are used to maintain internal state used to emulate block
46 * devices on top of zvols. In particular, management of device minor number
47 * operations - create, remove, rename, and set_snapdev - involves access to
48 * these structures. The zvol_state_lock is primarily used to protect the
49 * zvol_state_list. The zv->zv_state_lock is used to protect the contents
50 * of the zvol_state_t structures, as well as to make sure that when the
51 * time comes to remove the structure from the list, it is not in use, and
52 * therefore, it can be taken off zvol_state_list and freed.
53 *
54 * The zv_suspend_lock was introduced to allow for suspending I/O to a zvol,
55 * e.g. for the duration of receive and rollback operations. This lock can be
56 * held for significant periods of time. Given that it is undesirable to hold
57 * mutexes for long periods of time, the following lock ordering applies:
58 * - take zvol_state_lock if necessary, to protect zvol_state_list
59 * - take zv_suspend_lock if necessary, by the code path in question
60 * - take zv_state_lock to protect zvol_state_t
61 *
62 * The minor operations are issued to spa->spa_zvol_taskq queues, that are
63 * single-threaded (to preserve order of minor operations), and are executed
64 * through the zvol_task_cb that dispatches the specific operations. Therefore,
65 * these operations are serialized per pool. Consequently, we can be certain
66 * that for a given zvol, there is only one operation at a time in progress.
67 * That is why one can be sure that first, zvol_state_t for a given zvol is
68 * allocated and placed on zvol_state_list, and then other minor operations
69 * for this zvol are going to proceed in the order of issue.
70 *
71 */
72
73 #include <sys/dataset_kstats.h>
74 #include <sys/dbuf.h>
75 #include <sys/dmu_traverse.h>
76 #include <sys/dsl_dataset.h>
77 #include <sys/dsl_prop.h>
78 #include <sys/dsl_dir.h>
79 #include <sys/zap.h>
80 #include <sys/zfeature.h>
81 #include <sys/zil_impl.h>
82 #include <sys/dmu_tx.h>
83 #include <sys/zio.h>
84 #include <sys/zfs_rlock.h>
85 #include <sys/spa_impl.h>
86 #include <sys/zvol.h>
87 #include <sys/zvol_impl.h>
88
89 unsigned int zvol_inhibit_dev = 0;
90 unsigned int zvol_volmode = ZFS_VOLMODE_GEOM;
91
92 struct hlist_head *zvol_htable;
93 static list_t zvol_state_list;
94 krwlock_t zvol_state_lock;
95
96 typedef enum {
97 ZVOL_ASYNC_REMOVE_MINORS,
98 ZVOL_ASYNC_RENAME_MINORS,
99 ZVOL_ASYNC_SET_SNAPDEV,
100 ZVOL_ASYNC_SET_VOLMODE,
101 ZVOL_ASYNC_MAX
102 } zvol_async_op_t;
103
104 typedef struct {
105 zvol_async_op_t op;
106 char name1[MAXNAMELEN];
107 char name2[MAXNAMELEN];
108 uint64_t value;
109 } zvol_task_t;
110
111 uint64_t
112 zvol_name_hash(const char *name)
113 {
114 int i;
115 uint64_t crc = -1ULL;
116 const uint8_t *p = (const uint8_t *)name;
117 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
118 for (i = 0; i < MAXNAMELEN - 1 && *p; i++, p++) {
119 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (*p)) & 0xFF];
120 }
121 return (crc);
122 }
123
124 /*
125 * Find a zvol_state_t given the name and hash generated by zvol_name_hash.
126 * If found, return with zv_suspend_lock and zv_state_lock taken, otherwise,
127 * return (NULL) without the taking locks. The zv_suspend_lock is always taken
128 * before zv_state_lock. The mode argument indicates the mode (including none)
129 * for zv_suspend_lock to be taken.
130 */
131 zvol_state_t *
132 zvol_find_by_name_hash(const char *name, uint64_t hash, int mode)
133 {
134 zvol_state_t *zv;
135 struct hlist_node *p = NULL;
136
137 rw_enter(&zvol_state_lock, RW_READER);
138 hlist_for_each(p, ZVOL_HT_HEAD(hash)) {
139 zv = hlist_entry(p, zvol_state_t, zv_hlink);
140 mutex_enter(&zv->zv_state_lock);
141 if (zv->zv_hash == hash &&
142 strncmp(zv->zv_name, name, MAXNAMELEN) == 0) {
143 /*
144 * this is the right zvol, take the locks in the
145 * right order
146 */
147 if (mode != RW_NONE &&
148 !rw_tryenter(&zv->zv_suspend_lock, mode)) {
149 mutex_exit(&zv->zv_state_lock);
150 rw_enter(&zv->zv_suspend_lock, mode);
151 mutex_enter(&zv->zv_state_lock);
152 /*
153 * zvol cannot be renamed as we continue
154 * to hold zvol_state_lock
155 */
156 ASSERT(zv->zv_hash == hash &&
157 strncmp(zv->zv_name, name, MAXNAMELEN)
158 == 0);
159 }
160 rw_exit(&zvol_state_lock);
161 return (zv);
162 }
163 mutex_exit(&zv->zv_state_lock);
164 }
165 rw_exit(&zvol_state_lock);
166
167 return (NULL);
168 }
169
170 /*
171 * Find a zvol_state_t given the name.
172 * If found, return with zv_suspend_lock and zv_state_lock taken, otherwise,
173 * return (NULL) without the taking locks. The zv_suspend_lock is always taken
174 * before zv_state_lock. The mode argument indicates the mode (including none)
175 * for zv_suspend_lock to be taken.
176 */
177 static zvol_state_t *
178 zvol_find_by_name(const char *name, int mode)
179 {
180 return (zvol_find_by_name_hash(name, zvol_name_hash(name), mode));
181 }
182
183 /*
184 * ZFS_IOC_CREATE callback handles dmu zvol and zap object creation.
185 */
186 void
187 zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
188 {
189 zfs_creat_t *zct = arg;
190 nvlist_t *nvprops = zct->zct_props;
191 int error;
192 uint64_t volblocksize, volsize;
193
194 VERIFY(nvlist_lookup_uint64(nvprops,
195 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
196 if (nvlist_lookup_uint64(nvprops,
197 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
198 volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
199
200 /*
201 * These properties must be removed from the list so the generic
202 * property setting step won't apply to them.
203 */
204 VERIFY(nvlist_remove_all(nvprops,
205 zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
206 (void) nvlist_remove_all(nvprops,
207 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
208
209 error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
210 DMU_OT_NONE, 0, tx);
211 ASSERT(error == 0);
212
213 error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
214 DMU_OT_NONE, 0, tx);
215 ASSERT(error == 0);
216
217 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
218 ASSERT(error == 0);
219 }
220
221 /*
222 * ZFS_IOC_OBJSET_STATS entry point.
223 */
224 int
225 zvol_get_stats(objset_t *os, nvlist_t *nv)
226 {
227 int error;
228 dmu_object_info_t *doi;
229 uint64_t val;
230
231 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
232 if (error)
233 return (SET_ERROR(error));
234
235 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
236 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
237 error = dmu_object_info(os, ZVOL_OBJ, doi);
238
239 if (error == 0) {
240 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
241 doi->doi_data_block_size);
242 }
243
244 kmem_free(doi, sizeof (dmu_object_info_t));
245
246 return (SET_ERROR(error));
247 }
248
249 /*
250 * Sanity check volume size.
251 */
252 int
253 zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
254 {
255 if (volsize == 0)
256 return (SET_ERROR(EINVAL));
257
258 if (volsize % blocksize != 0)
259 return (SET_ERROR(EINVAL));
260
261 #ifdef _ILP32
262 if (volsize - 1 > SPEC_MAXOFFSET_T)
263 return (SET_ERROR(EOVERFLOW));
264 #endif
265 return (0);
266 }
267
268 /*
269 * Ensure the zap is flushed then inform the VFS of the capacity change.
270 */
271 static int
272 zvol_update_volsize(uint64_t volsize, objset_t *os)
273 {
274 dmu_tx_t *tx;
275 int error;
276 uint64_t txg;
277
278 tx = dmu_tx_create(os);
279 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
280 dmu_tx_mark_netfree(tx);
281 error = dmu_tx_assign(tx, TXG_WAIT);
282 if (error) {
283 dmu_tx_abort(tx);
284 return (SET_ERROR(error));
285 }
286 txg = dmu_tx_get_txg(tx);
287
288 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
289 &volsize, tx);
290 dmu_tx_commit(tx);
291
292 txg_wait_synced(dmu_objset_pool(os), txg);
293
294 if (error == 0)
295 error = dmu_free_long_range(os,
296 ZVOL_OBJ, volsize, DMU_OBJECT_END);
297
298 return (error);
299 }
300
301 /*
302 * Set ZFS_PROP_VOLSIZE set entry point. Note that modifying the volume
303 * size will result in a udev "change" event being generated.
304 */
305 int
306 zvol_set_volsize(const char *name, uint64_t volsize)
307 {
308 objset_t *os = NULL;
309 uint64_t readonly;
310 int error;
311 boolean_t owned = B_FALSE;
312
313 error = dsl_prop_get_integer(name,
314 zfs_prop_to_name(ZFS_PROP_READONLY), &readonly, NULL);
315 if (error != 0)
316 return (SET_ERROR(error));
317 if (readonly)
318 return (SET_ERROR(EROFS));
319
320 zvol_state_t *zv = zvol_find_by_name(name, RW_READER);
321
322 ASSERT(zv == NULL || (MUTEX_HELD(&zv->zv_state_lock) &&
323 RW_READ_HELD(&zv->zv_suspend_lock)));
324
325 if (zv == NULL || zv->zv_objset == NULL) {
326 if (zv != NULL)
327 rw_exit(&zv->zv_suspend_lock);
328 if ((error = dmu_objset_own(name, DMU_OST_ZVOL, B_FALSE, B_TRUE,
329 FTAG, &os)) != 0) {
330 if (zv != NULL)
331 mutex_exit(&zv->zv_state_lock);
332 return (SET_ERROR(error));
333 }
334 owned = B_TRUE;
335 if (zv != NULL)
336 zv->zv_objset = os;
337 } else {
338 os = zv->zv_objset;
339 }
340
341 dmu_object_info_t *doi = kmem_alloc(sizeof (*doi), KM_SLEEP);
342
343 if ((error = dmu_object_info(os, ZVOL_OBJ, doi)) ||
344 (error = zvol_check_volsize(volsize, doi->doi_data_block_size)))
345 goto out;
346
347 error = zvol_update_volsize(volsize, os);
348 if (error == 0 && zv != NULL) {
349 zv->zv_volsize = volsize;
350 zv->zv_changed = 1;
351 }
352 out:
353 kmem_free(doi, sizeof (dmu_object_info_t));
354
355 if (owned) {
356 dmu_objset_disown(os, B_TRUE, FTAG);
357 if (zv != NULL)
358 zv->zv_objset = NULL;
359 } else {
360 rw_exit(&zv->zv_suspend_lock);
361 }
362
363 if (zv != NULL)
364 mutex_exit(&zv->zv_state_lock);
365
366 if (error == 0 && zv != NULL)
367 zvol_os_update_volsize(zv, volsize);
368
369 return (SET_ERROR(error));
370 }
371
372 /*
373 * Sanity check volume block size.
374 */
375 int
376 zvol_check_volblocksize(const char *name, uint64_t volblocksize)
377 {
378 /* Record sizes above 128k need the feature to be enabled */
379 if (volblocksize > SPA_OLD_MAXBLOCKSIZE) {
380 spa_t *spa;
381 int error;
382
383 if ((error = spa_open(name, &spa, FTAG)) != 0)
384 return (error);
385
386 if (!spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) {
387 spa_close(spa, FTAG);
388 return (SET_ERROR(ENOTSUP));
389 }
390
391 /*
392 * We don't allow setting the property above 1MB,
393 * unless the tunable has been changed.
394 */
395 if (volblocksize > zfs_max_recordsize)
396 return (SET_ERROR(EDOM));
397
398 spa_close(spa, FTAG);
399 }
400
401 if (volblocksize < SPA_MINBLOCKSIZE ||
402 volblocksize > SPA_MAXBLOCKSIZE ||
403 !ISP2(volblocksize))
404 return (SET_ERROR(EDOM));
405
406 return (0);
407 }
408
409 /*
410 * Replay a TX_TRUNCATE ZIL transaction if asked. TX_TRUNCATE is how we
411 * implement DKIOCFREE/free-long-range.
412 */
413 static int
414 zvol_replay_truncate(void *arg1, void *arg2, boolean_t byteswap)
415 {
416 zvol_state_t *zv = arg1;
417 lr_truncate_t *lr = arg2;
418 uint64_t offset, length;
419
420 if (byteswap)
421 byteswap_uint64_array(lr, sizeof (*lr));
422
423 offset = lr->lr_offset;
424 length = lr->lr_length;
425
426 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
427 dmu_tx_mark_netfree(tx);
428 int error = dmu_tx_assign(tx, TXG_WAIT);
429 if (error != 0) {
430 dmu_tx_abort(tx);
431 } else {
432 (void) zil_replaying(zv->zv_zilog, tx);
433 dmu_tx_commit(tx);
434 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, offset,
435 length);
436 }
437
438 return (error);
439 }
440
441 /*
442 * Replay a TX_WRITE ZIL transaction that didn't get committed
443 * after a system failure
444 */
445 static int
446 zvol_replay_write(void *arg1, void *arg2, boolean_t byteswap)
447 {
448 zvol_state_t *zv = arg1;
449 lr_write_t *lr = arg2;
450 objset_t *os = zv->zv_objset;
451 char *data = (char *)(lr + 1); /* data follows lr_write_t */
452 uint64_t offset, length;
453 dmu_tx_t *tx;
454 int error;
455
456 if (byteswap)
457 byteswap_uint64_array(lr, sizeof (*lr));
458
459 offset = lr->lr_offset;
460 length = lr->lr_length;
461
462 /* If it's a dmu_sync() block, write the whole block */
463 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
464 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
465 if (length < blocksize) {
466 offset -= offset % blocksize;
467 length = blocksize;
468 }
469 }
470
471 tx = dmu_tx_create(os);
472 dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length);
473 error = dmu_tx_assign(tx, TXG_WAIT);
474 if (error) {
475 dmu_tx_abort(tx);
476 } else {
477 dmu_write(os, ZVOL_OBJ, offset, length, data, tx);
478 (void) zil_replaying(zv->zv_zilog, tx);
479 dmu_tx_commit(tx);
480 }
481
482 return (error);
483 }
484
485 /*
486 * Replay a TX_CLONE_RANGE ZIL transaction that didn't get committed
487 * after a system failure.
488 *
489 * TODO: For now we drop block cloning transations for ZVOLs as they are
490 * unsupported, but we still need to inform BRT about that as we
491 * claimed them during pool import.
492 * This situation can occur when we try to import a pool from a ZFS
493 * version supporting block cloning for ZVOLs into a system that
494 * has this ZFS version, that doesn't support block cloning for ZVOLs.
495 */
496 static int
497 zvol_replay_clone_range(void *arg1, void *arg2, boolean_t byteswap)
498 {
499 char name[ZFS_MAX_DATASET_NAME_LEN];
500 zvol_state_t *zv = arg1;
501 objset_t *os = zv->zv_objset;
502 lr_clone_range_t *lr = arg2;
503 blkptr_t *bp;
504 dmu_tx_t *tx;
505 spa_t *spa;
506 uint_t ii;
507 int error;
508
509 dmu_objset_name(os, name);
510 cmn_err(CE_WARN, "ZFS dropping block cloning transaction for %s.",
511 name);
512
513 if (byteswap)
514 byteswap_uint64_array(lr, sizeof (*lr));
515
516 tx = dmu_tx_create(os);
517 error = dmu_tx_assign(tx, TXG_WAIT);
518 if (error) {
519 dmu_tx_abort(tx);
520 return (error);
521 }
522
523 spa = os->os_spa;
524
525 for (ii = 0; ii < lr->lr_nbps; ii++) {
526 bp = &lr->lr_bps[ii];
527
528 if (!BP_IS_HOLE(bp)) {
529 zio_free(spa, dmu_tx_get_txg(tx), bp);
530 }
531 }
532
533 (void) zil_replaying(zv->zv_zilog, tx);
534 dmu_tx_commit(tx);
535
536 return (0);
537 }
538
539 static int
540 zvol_replay_err(void *arg1, void *arg2, boolean_t byteswap)
541 {
542 (void) arg1, (void) arg2, (void) byteswap;
543 return (SET_ERROR(ENOTSUP));
544 }
545
546 /*
547 * Callback vectors for replaying records.
548 * Only TX_WRITE and TX_TRUNCATE are needed for zvol.
549 */
550 zil_replay_func_t *const zvol_replay_vector[TX_MAX_TYPE] = {
551 zvol_replay_err, /* no such transaction type */
552 zvol_replay_err, /* TX_CREATE */
553 zvol_replay_err, /* TX_MKDIR */
554 zvol_replay_err, /* TX_MKXATTR */
555 zvol_replay_err, /* TX_SYMLINK */
556 zvol_replay_err, /* TX_REMOVE */
557 zvol_replay_err, /* TX_RMDIR */
558 zvol_replay_err, /* TX_LINK */
559 zvol_replay_err, /* TX_RENAME */
560 zvol_replay_write, /* TX_WRITE */
561 zvol_replay_truncate, /* TX_TRUNCATE */
562 zvol_replay_err, /* TX_SETATTR */
563 zvol_replay_err, /* TX_ACL */
564 zvol_replay_err, /* TX_CREATE_ATTR */
565 zvol_replay_err, /* TX_CREATE_ACL_ATTR */
566 zvol_replay_err, /* TX_MKDIR_ACL */
567 zvol_replay_err, /* TX_MKDIR_ATTR */
568 zvol_replay_err, /* TX_MKDIR_ACL_ATTR */
569 zvol_replay_err, /* TX_WRITE2 */
570 zvol_replay_err, /* TX_SETSAXATTR */
571 zvol_replay_err, /* TX_RENAME_EXCHANGE */
572 zvol_replay_err, /* TX_RENAME_WHITEOUT */
573 zvol_replay_clone_range /* TX_CLONE_RANGE */
574 };
575
576 /*
577 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
578 *
579 * We store data in the log buffers if it's small enough.
580 * Otherwise we will later flush the data out via dmu_sync().
581 */
582 static const ssize_t zvol_immediate_write_sz = 32768;
583
584 void
585 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, uint64_t offset,
586 uint64_t size, int sync)
587 {
588 uint32_t blocksize = zv->zv_volblocksize;
589 zilog_t *zilog = zv->zv_zilog;
590 itx_wr_state_t write_state;
591 uint64_t sz = size;
592
593 if (zil_replaying(zilog, tx))
594 return;
595
596 if (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
597 write_state = WR_INDIRECT;
598 else if (!spa_has_slogs(zilog->zl_spa) &&
599 size >= blocksize && blocksize > zvol_immediate_write_sz)
600 write_state = WR_INDIRECT;
601 else if (sync)
602 write_state = WR_COPIED;
603 else
604 write_state = WR_NEED_COPY;
605
606 while (size) {
607 itx_t *itx;
608 lr_write_t *lr;
609 itx_wr_state_t wr_state = write_state;
610 ssize_t len = size;
611
612 if (wr_state == WR_COPIED && size > zil_max_copied_data(zilog))
613 wr_state = WR_NEED_COPY;
614 else if (wr_state == WR_INDIRECT)
615 len = MIN(blocksize - P2PHASE(offset, blocksize), size);
616
617 itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
618 (wr_state == WR_COPIED ? len : 0));
619 lr = (lr_write_t *)&itx->itx_lr;
620 if (wr_state == WR_COPIED && dmu_read_by_dnode(zv->zv_dn,
621 offset, len, lr+1, DMU_READ_NO_PREFETCH) != 0) {
622 zil_itx_destroy(itx);
623 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
624 lr = (lr_write_t *)&itx->itx_lr;
625 wr_state = WR_NEED_COPY;
626 }
627
628 itx->itx_wr_state = wr_state;
629 lr->lr_foid = ZVOL_OBJ;
630 lr->lr_offset = offset;
631 lr->lr_length = len;
632 lr->lr_blkoff = 0;
633 BP_ZERO(&lr->lr_blkptr);
634
635 itx->itx_private = zv;
636 itx->itx_sync = sync;
637
638 (void) zil_itx_assign(zilog, itx, tx);
639
640 offset += len;
641 size -= len;
642 }
643
644 if (write_state == WR_COPIED || write_state == WR_NEED_COPY) {
645 dsl_pool_wrlog_count(zilog->zl_dmu_pool, sz, tx->tx_txg);
646 }
647 }
648
649 /*
650 * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE.
651 */
652 void
653 zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off, uint64_t len,
654 boolean_t sync)
655 {
656 itx_t *itx;
657 lr_truncate_t *lr;
658 zilog_t *zilog = zv->zv_zilog;
659
660 if (zil_replaying(zilog, tx))
661 return;
662
663 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
664 lr = (lr_truncate_t *)&itx->itx_lr;
665 lr->lr_foid = ZVOL_OBJ;
666 lr->lr_offset = off;
667 lr->lr_length = len;
668
669 itx->itx_sync = sync;
670 zil_itx_assign(zilog, itx, tx);
671 }
672
673
674 static void
675 zvol_get_done(zgd_t *zgd, int error)
676 {
677 (void) error;
678 if (zgd->zgd_db)
679 dmu_buf_rele(zgd->zgd_db, zgd);
680
681 zfs_rangelock_exit(zgd->zgd_lr);
682
683 kmem_free(zgd, sizeof (zgd_t));
684 }
685
686 /*
687 * Get data to generate a TX_WRITE intent log record.
688 */
689 int
690 zvol_get_data(void *arg, uint64_t arg2, lr_write_t *lr, char *buf,
691 struct lwb *lwb, zio_t *zio)
692 {
693 zvol_state_t *zv = arg;
694 uint64_t offset = lr->lr_offset;
695 uint64_t size = lr->lr_length;
696 dmu_buf_t *db;
697 zgd_t *zgd;
698 int error;
699
700 ASSERT3P(lwb, !=, NULL);
701 ASSERT3P(zio, !=, NULL);
702 ASSERT3U(size, !=, 0);
703
704 zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
705 zgd->zgd_lwb = lwb;
706
707 /*
708 * Write records come in two flavors: immediate and indirect.
709 * For small writes it's cheaper to store the data with the
710 * log record (immediate); for large writes it's cheaper to
711 * sync the data and get a pointer to it (indirect) so that
712 * we don't have to write the data twice.
713 */
714 if (buf != NULL) { /* immediate write */
715 zgd->zgd_lr = zfs_rangelock_enter(&zv->zv_rangelock, offset,
716 size, RL_READER);
717 error = dmu_read_by_dnode(zv->zv_dn, offset, size, buf,
718 DMU_READ_NO_PREFETCH);
719 } else { /* indirect write */
720 /*
721 * Have to lock the whole block to ensure when it's written out
722 * and its checksum is being calculated that no one can change
723 * the data. Contrarily to zfs_get_data we need not re-check
724 * blocksize after we get the lock because it cannot be changed.
725 */
726 size = zv->zv_volblocksize;
727 offset = P2ALIGN_TYPED(offset, size, uint64_t);
728 zgd->zgd_lr = zfs_rangelock_enter(&zv->zv_rangelock, offset,
729 size, RL_READER);
730 error = dmu_buf_hold_by_dnode(zv->zv_dn, offset, zgd, &db,
731 DMU_READ_NO_PREFETCH);
732 if (error == 0) {
733 blkptr_t *bp = &lr->lr_blkptr;
734
735 zgd->zgd_db = db;
736 zgd->zgd_bp = bp;
737
738 ASSERT(db != NULL);
739 ASSERT(db->db_offset == offset);
740 ASSERT(db->db_size == size);
741
742 error = dmu_sync(zio, lr->lr_common.lrc_txg,
743 zvol_get_done, zgd);
744
745 if (error == 0)
746 return (0);
747 }
748 }
749
750 zvol_get_done(zgd, error);
751
752 return (SET_ERROR(error));
753 }
754
755 /*
756 * The zvol_state_t's are inserted into zvol_state_list and zvol_htable.
757 */
758
759 void
760 zvol_insert(zvol_state_t *zv)
761 {
762 ASSERT(RW_WRITE_HELD(&zvol_state_lock));
763 list_insert_head(&zvol_state_list, zv);
764 hlist_add_head(&zv->zv_hlink, ZVOL_HT_HEAD(zv->zv_hash));
765 }
766
767 /*
768 * Simply remove the zvol from to list of zvols.
769 */
770 static void
771 zvol_remove(zvol_state_t *zv)
772 {
773 ASSERT(RW_WRITE_HELD(&zvol_state_lock));
774 list_remove(&zvol_state_list, zv);
775 hlist_del(&zv->zv_hlink);
776 }
777
778 /*
779 * Setup zv after we just own the zv->objset
780 */
781 static int
782 zvol_setup_zv(zvol_state_t *zv)
783 {
784 uint64_t volsize;
785 int error;
786 uint64_t ro;
787 objset_t *os = zv->zv_objset;
788
789 ASSERT(MUTEX_HELD(&zv->zv_state_lock));
790 ASSERT(RW_LOCK_HELD(&zv->zv_suspend_lock));
791
792 zv->zv_zilog = NULL;
793 zv->zv_flags &= ~ZVOL_WRITTEN_TO;
794
795 error = dsl_prop_get_integer(zv->zv_name, "readonly", &ro, NULL);
796 if (error)
797 return (SET_ERROR(error));
798
799 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
800 if (error)
801 return (SET_ERROR(error));
802
803 error = dnode_hold(os, ZVOL_OBJ, zv, &zv->zv_dn);
804 if (error)
805 return (SET_ERROR(error));
806
807 zvol_os_set_capacity(zv, volsize >> 9);
808 zv->zv_volsize = volsize;
809
810 if (ro || dmu_objset_is_snapshot(os) ||
811 !spa_writeable(dmu_objset_spa(os))) {
812 zvol_os_set_disk_ro(zv, 1);
813 zv->zv_flags |= ZVOL_RDONLY;
814 } else {
815 zvol_os_set_disk_ro(zv, 0);
816 zv->zv_flags &= ~ZVOL_RDONLY;
817 }
818 return (0);
819 }
820
821 /*
822 * Shutdown every zv_objset related stuff except zv_objset itself.
823 * The is the reverse of zvol_setup_zv.
824 */
825 static void
826 zvol_shutdown_zv(zvol_state_t *zv)
827 {
828 ASSERT(MUTEX_HELD(&zv->zv_state_lock) &&
829 RW_LOCK_HELD(&zv->zv_suspend_lock));
830
831 if (zv->zv_flags & ZVOL_WRITTEN_TO) {
832 ASSERT(zv->zv_zilog != NULL);
833 zil_close(zv->zv_zilog);
834 }
835
836 zv->zv_zilog = NULL;
837
838 dnode_rele(zv->zv_dn, zv);
839 zv->zv_dn = NULL;
840
841 /*
842 * Evict cached data. We must write out any dirty data before
843 * disowning the dataset.
844 */
845 if (zv->zv_flags & ZVOL_WRITTEN_TO)
846 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
847 (void) dmu_objset_evict_dbufs(zv->zv_objset);
848 }
849
850 /*
851 * return the proper tag for rollback and recv
852 */
853 void *
854 zvol_tag(zvol_state_t *zv)
855 {
856 ASSERT(RW_WRITE_HELD(&zv->zv_suspend_lock));
857 return (zv->zv_open_count > 0 ? zv : NULL);
858 }
859
860 /*
861 * Suspend the zvol for recv and rollback.
862 */
863 zvol_state_t *
864 zvol_suspend(const char *name)
865 {
866 zvol_state_t *zv;
867
868 zv = zvol_find_by_name(name, RW_WRITER);
869
870 if (zv == NULL)
871 return (NULL);
872
873 /* block all I/O, release in zvol_resume. */
874 ASSERT(MUTEX_HELD(&zv->zv_state_lock));
875 ASSERT(RW_WRITE_HELD(&zv->zv_suspend_lock));
876
877 atomic_inc(&zv->zv_suspend_ref);
878
879 if (zv->zv_open_count > 0)
880 zvol_shutdown_zv(zv);
881
882 /*
883 * do not hold zv_state_lock across suspend/resume to
884 * avoid locking up zvol lookups
885 */
886 mutex_exit(&zv->zv_state_lock);
887
888 /* zv_suspend_lock is released in zvol_resume() */
889 return (zv);
890 }
891
892 int
893 zvol_resume(zvol_state_t *zv)
894 {
895 int error = 0;
896
897 ASSERT(RW_WRITE_HELD(&zv->zv_suspend_lock));
898
899 mutex_enter(&zv->zv_state_lock);
900
901 if (zv->zv_open_count > 0) {
902 VERIFY0(dmu_objset_hold(zv->zv_name, zv, &zv->zv_objset));
903 VERIFY3P(zv->zv_objset->os_dsl_dataset->ds_owner, ==, zv);
904 VERIFY(dsl_dataset_long_held(zv->zv_objset->os_dsl_dataset));
905 dmu_objset_rele(zv->zv_objset, zv);
906
907 error = zvol_setup_zv(zv);
908 }
909
910 mutex_exit(&zv->zv_state_lock);
911
912 rw_exit(&zv->zv_suspend_lock);
913 /*
914 * We need this because we don't hold zvol_state_lock while releasing
915 * zv_suspend_lock. zvol_remove_minors_impl thus cannot check
916 * zv_suspend_lock to determine it is safe to free because rwlock is
917 * not inherent atomic.
918 */
919 atomic_dec(&zv->zv_suspend_ref);
920
921 return (SET_ERROR(error));
922 }
923
924 int
925 zvol_first_open(zvol_state_t *zv, boolean_t readonly)
926 {
927 objset_t *os;
928 int error;
929
930 ASSERT(RW_READ_HELD(&zv->zv_suspend_lock));
931 ASSERT(MUTEX_HELD(&zv->zv_state_lock));
932 ASSERT(mutex_owned(&spa_namespace_lock));
933
934 boolean_t ro = (readonly || (strchr(zv->zv_name, '@') != NULL));
935 error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, ro, B_TRUE, zv, &os);
936 if (error)
937 return (SET_ERROR(error));
938
939 zv->zv_objset = os;
940
941 error = zvol_setup_zv(zv);
942 if (error) {
943 dmu_objset_disown(os, 1, zv);
944 zv->zv_objset = NULL;
945 }
946
947 return (error);
948 }
949
950 void
951 zvol_last_close(zvol_state_t *zv)
952 {
953 ASSERT(RW_READ_HELD(&zv->zv_suspend_lock));
954 ASSERT(MUTEX_HELD(&zv->zv_state_lock));
955
956 zvol_shutdown_zv(zv);
957
958 dmu_objset_disown(zv->zv_objset, 1, zv);
959 zv->zv_objset = NULL;
960 }
961
962 typedef struct minors_job {
963 list_t *list;
964 list_node_t link;
965 /* input */
966 char *name;
967 /* output */
968 int error;
969 } minors_job_t;
970
971 /*
972 * Prefetch zvol dnodes for the minors_job
973 */
974 static void
975 zvol_prefetch_minors_impl(void *arg)
976 {
977 minors_job_t *job = arg;
978 char *dsname = job->name;
979 objset_t *os = NULL;
980
981 job->error = dmu_objset_own(dsname, DMU_OST_ZVOL, B_TRUE, B_TRUE,
982 FTAG, &os);
983 if (job->error == 0) {
984 dmu_prefetch(os, ZVOL_OBJ, 0, 0, 0, ZIO_PRIORITY_SYNC_READ);
985 dmu_objset_disown(os, B_TRUE, FTAG);
986 }
987 }
988
989 /*
990 * Mask errors to continue dmu_objset_find() traversal
991 */
992 static int
993 zvol_create_snap_minor_cb(const char *dsname, void *arg)
994 {
995 minors_job_t *j = arg;
996 list_t *minors_list = j->list;
997 const char *name = j->name;
998
999 ASSERT0(MUTEX_HELD(&spa_namespace_lock));
1000
1001 /* skip the designated dataset */
1002 if (name && strcmp(dsname, name) == 0)
1003 return (0);
1004
1005 /* at this point, the dsname should name a snapshot */
1006 if (strchr(dsname, '@') == 0) {
1007 dprintf("zvol_create_snap_minor_cb(): "
1008 "%s is not a snapshot name\n", dsname);
1009 } else {
1010 minors_job_t *job;
1011 char *n = kmem_strdup(dsname);
1012 if (n == NULL)
1013 return (0);
1014
1015 job = kmem_alloc(sizeof (minors_job_t), KM_SLEEP);
1016 job->name = n;
1017 job->list = minors_list;
1018 job->error = 0;
1019 list_insert_tail(minors_list, job);
1020 /* don't care if dispatch fails, because job->error is 0 */
1021 taskq_dispatch(system_taskq, zvol_prefetch_minors_impl, job,
1022 TQ_SLEEP);
1023 }
1024
1025 return (0);
1026 }
1027
1028 /*
1029 * If spa_keystore_load_wkey() is called for an encrypted zvol,
1030 * we need to look for any clones also using the key. This function
1031 * is "best effort" - so we just skip over it if there are failures.
1032 */
1033 static void
1034 zvol_add_clones(const char *dsname, list_t *minors_list)
1035 {
1036 /* Also check if it has clones */
1037 dsl_dir_t *dd = NULL;
1038 dsl_pool_t *dp = NULL;
1039
1040 if (dsl_pool_hold(dsname, FTAG, &dp) != 0)
1041 return;
1042
1043 if (!spa_feature_is_enabled(dp->dp_spa,
1044 SPA_FEATURE_ENCRYPTION))
1045 goto out;
1046
1047 if (dsl_dir_hold(dp, dsname, FTAG, &dd, NULL) != 0)
1048 goto out;
1049
1050 if (dsl_dir_phys(dd)->dd_clones == 0)
1051 goto out;
1052
1053 zap_cursor_t *zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP);
1054 zap_attribute_t *za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1055 objset_t *mos = dd->dd_pool->dp_meta_objset;
1056
1057 for (zap_cursor_init(zc, mos, dsl_dir_phys(dd)->dd_clones);
1058 zap_cursor_retrieve(zc, za) == 0;
1059 zap_cursor_advance(zc)) {
1060 dsl_dataset_t *clone;
1061 minors_job_t *job;
1062
1063 if (dsl_dataset_hold_obj(dd->dd_pool,
1064 za->za_first_integer, FTAG, &clone) == 0) {
1065
1066 char name[ZFS_MAX_DATASET_NAME_LEN];
1067 dsl_dataset_name(clone, name);
1068
1069 char *n = kmem_strdup(name);
1070 job = kmem_alloc(sizeof (minors_job_t), KM_SLEEP);
1071 job->name = n;
1072 job->list = minors_list;
1073 job->error = 0;
1074 list_insert_tail(minors_list, job);
1075
1076 dsl_dataset_rele(clone, FTAG);
1077 }
1078 }
1079 zap_cursor_fini(zc);
1080 kmem_free(za, sizeof (zap_attribute_t));
1081 kmem_free(zc, sizeof (zap_cursor_t));
1082
1083 out:
1084 if (dd != NULL)
1085 dsl_dir_rele(dd, FTAG);
1086 dsl_pool_rele(dp, FTAG);
1087 }
1088
1089 /*
1090 * Mask errors to continue dmu_objset_find() traversal
1091 */
1092 static int
1093 zvol_create_minors_cb(const char *dsname, void *arg)
1094 {
1095 uint64_t snapdev;
1096 int error;
1097 list_t *minors_list = arg;
1098
1099 ASSERT0(MUTEX_HELD(&spa_namespace_lock));
1100
1101 error = dsl_prop_get_integer(dsname, "snapdev", &snapdev, NULL);
1102 if (error)
1103 return (0);
1104
1105 /*
1106 * Given the name and the 'snapdev' property, create device minor nodes
1107 * with the linkages to zvols/snapshots as needed.
1108 * If the name represents a zvol, create a minor node for the zvol, then
1109 * check if its snapshots are 'visible', and if so, iterate over the
1110 * snapshots and create device minor nodes for those.
1111 */
1112 if (strchr(dsname, '@') == 0) {
1113 minors_job_t *job;
1114 char *n = kmem_strdup(dsname);
1115 if (n == NULL)
1116 return (0);
1117
1118 job = kmem_alloc(sizeof (minors_job_t), KM_SLEEP);
1119 job->name = n;
1120 job->list = minors_list;
1121 job->error = 0;
1122 list_insert_tail(minors_list, job);
1123 /* don't care if dispatch fails, because job->error is 0 */
1124 taskq_dispatch(system_taskq, zvol_prefetch_minors_impl, job,
1125 TQ_SLEEP);
1126
1127 zvol_add_clones(dsname, minors_list);
1128
1129 if (snapdev == ZFS_SNAPDEV_VISIBLE) {
1130 /*
1131 * traverse snapshots only, do not traverse children,
1132 * and skip the 'dsname'
1133 */
1134 (void) dmu_objset_find(dsname,
1135 zvol_create_snap_minor_cb, (void *)job,
1136 DS_FIND_SNAPSHOTS);
1137 }
1138 } else {
1139 dprintf("zvol_create_minors_cb(): %s is not a zvol name\n",
1140 dsname);
1141 }
1142
1143 return (0);
1144 }
1145
1146 /*
1147 * Create minors for the specified dataset, including children and snapshots.
1148 * Pay attention to the 'snapdev' property and iterate over the snapshots
1149 * only if they are 'visible'. This approach allows one to assure that the
1150 * snapshot metadata is read from disk only if it is needed.
1151 *
1152 * The name can represent a dataset to be recursively scanned for zvols and
1153 * their snapshots, or a single zvol snapshot. If the name represents a
1154 * dataset, the scan is performed in two nested stages:
1155 * - scan the dataset for zvols, and
1156 * - for each zvol, create a minor node, then check if the zvol's snapshots
1157 * are 'visible', and only then iterate over the snapshots if needed
1158 *
1159 * If the name represents a snapshot, a check is performed if the snapshot is
1160 * 'visible' (which also verifies that the parent is a zvol), and if so,
1161 * a minor node for that snapshot is created.
1162 */
1163 void
1164 zvol_create_minors_recursive(const char *name)
1165 {
1166 list_t minors_list;
1167 minors_job_t *job;
1168
1169 if (zvol_inhibit_dev)
1170 return;
1171
1172 /*
1173 * This is the list for prefetch jobs. Whenever we found a match
1174 * during dmu_objset_find, we insert a minors_job to the list and do
1175 * taskq_dispatch to parallel prefetch zvol dnodes. Note we don't need
1176 * any lock because all list operation is done on the current thread.
1177 *
1178 * We will use this list to do zvol_os_create_minor after prefetch
1179 * so we don't have to traverse using dmu_objset_find again.
1180 */
1181 list_create(&minors_list, sizeof (minors_job_t),
1182 offsetof(minors_job_t, link));
1183
1184
1185 if (strchr(name, '@') != NULL) {
1186 uint64_t snapdev;
1187
1188 int error = dsl_prop_get_integer(name, "snapdev",
1189 &snapdev, NULL);
1190
1191 if (error == 0 && snapdev == ZFS_SNAPDEV_VISIBLE)
1192 (void) zvol_os_create_minor(name);
1193 } else {
1194 fstrans_cookie_t cookie = spl_fstrans_mark();
1195 (void) dmu_objset_find(name, zvol_create_minors_cb,
1196 &minors_list, DS_FIND_CHILDREN);
1197 spl_fstrans_unmark(cookie);
1198 }
1199
1200 taskq_wait_outstanding(system_taskq, 0);
1201
1202 /*
1203 * Prefetch is completed, we can do zvol_os_create_minor
1204 * sequentially.
1205 */
1206 while ((job = list_head(&minors_list)) != NULL) {
1207 list_remove(&minors_list, job);
1208 if (!job->error)
1209 (void) zvol_os_create_minor(job->name);
1210 kmem_strfree(job->name);
1211 kmem_free(job, sizeof (minors_job_t));
1212 }
1213
1214 list_destroy(&minors_list);
1215 }
1216
1217 void
1218 zvol_create_minor(const char *name)
1219 {
1220 /*
1221 * Note: the dsl_pool_config_lock must not be held.
1222 * Minor node creation needs to obtain the zvol_state_lock.
1223 * zvol_open() obtains the zvol_state_lock and then the dsl pool
1224 * config lock. Therefore, we can't have the config lock now if
1225 * we are going to wait for the zvol_state_lock, because it
1226 * would be a lock order inversion which could lead to deadlock.
1227 */
1228
1229 if (zvol_inhibit_dev)
1230 return;
1231
1232 if (strchr(name, '@') != NULL) {
1233 uint64_t snapdev;
1234
1235 int error = dsl_prop_get_integer(name,
1236 "snapdev", &snapdev, NULL);
1237
1238 if (error == 0 && snapdev == ZFS_SNAPDEV_VISIBLE)
1239 (void) zvol_os_create_minor(name);
1240 } else {
1241 (void) zvol_os_create_minor(name);
1242 }
1243 }
1244
1245 /*
1246 * Remove minors for specified dataset including children and snapshots.
1247 */
1248
1249 static void
1250 zvol_free_task(void *arg)
1251 {
1252 zvol_os_free(arg);
1253 }
1254
1255 void
1256 zvol_remove_minors_impl(const char *name)
1257 {
1258 zvol_state_t *zv, *zv_next;
1259 int namelen = ((name) ? strlen(name) : 0);
1260 taskqid_t t;
1261 list_t free_list;
1262
1263 if (zvol_inhibit_dev)
1264 return;
1265
1266 list_create(&free_list, sizeof (zvol_state_t),
1267 offsetof(zvol_state_t, zv_next));
1268
1269 rw_enter(&zvol_state_lock, RW_WRITER);
1270
1271 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1272 zv_next = list_next(&zvol_state_list, zv);
1273
1274 mutex_enter(&zv->zv_state_lock);
1275 if (name == NULL || strcmp(zv->zv_name, name) == 0 ||
1276 (strncmp(zv->zv_name, name, namelen) == 0 &&
1277 (zv->zv_name[namelen] == '/' ||
1278 zv->zv_name[namelen] == '@'))) {
1279 /*
1280 * By holding zv_state_lock here, we guarantee that no
1281 * one is currently using this zv
1282 */
1283
1284 /* If in use, leave alone */
1285 if (zv->zv_open_count > 0 ||
1286 atomic_read(&zv->zv_suspend_ref)) {
1287 mutex_exit(&zv->zv_state_lock);
1288 continue;
1289 }
1290
1291 zvol_remove(zv);
1292
1293 /*
1294 * Cleared while holding zvol_state_lock as a writer
1295 * which will prevent zvol_open() from opening it.
1296 */
1297 zvol_os_clear_private(zv);
1298
1299 /* Drop zv_state_lock before zvol_free() */
1300 mutex_exit(&zv->zv_state_lock);
1301
1302 /* Try parallel zv_free, if failed do it in place */
1303 t = taskq_dispatch(system_taskq, zvol_free_task, zv,
1304 TQ_SLEEP);
1305 if (t == TASKQID_INVALID)
1306 list_insert_head(&free_list, zv);
1307 } else {
1308 mutex_exit(&zv->zv_state_lock);
1309 }
1310 }
1311 rw_exit(&zvol_state_lock);
1312
1313 /* Drop zvol_state_lock before calling zvol_free() */
1314 while ((zv = list_head(&free_list)) != NULL) {
1315 list_remove(&free_list, zv);
1316 zvol_os_free(zv);
1317 }
1318 }
1319
1320 /* Remove minor for this specific volume only */
1321 static void
1322 zvol_remove_minor_impl(const char *name)
1323 {
1324 zvol_state_t *zv = NULL, *zv_next;
1325
1326 if (zvol_inhibit_dev)
1327 return;
1328
1329 rw_enter(&zvol_state_lock, RW_WRITER);
1330
1331 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1332 zv_next = list_next(&zvol_state_list, zv);
1333
1334 mutex_enter(&zv->zv_state_lock);
1335 if (strcmp(zv->zv_name, name) == 0) {
1336 /*
1337 * By holding zv_state_lock here, we guarantee that no
1338 * one is currently using this zv
1339 */
1340
1341 /* If in use, leave alone */
1342 if (zv->zv_open_count > 0 ||
1343 atomic_read(&zv->zv_suspend_ref)) {
1344 mutex_exit(&zv->zv_state_lock);
1345 continue;
1346 }
1347 zvol_remove(zv);
1348
1349 zvol_os_clear_private(zv);
1350 mutex_exit(&zv->zv_state_lock);
1351 break;
1352 } else {
1353 mutex_exit(&zv->zv_state_lock);
1354 }
1355 }
1356
1357 /* Drop zvol_state_lock before calling zvol_free() */
1358 rw_exit(&zvol_state_lock);
1359
1360 if (zv != NULL)
1361 zvol_os_free(zv);
1362 }
1363
1364 /*
1365 * Rename minors for specified dataset including children and snapshots.
1366 */
1367 static void
1368 zvol_rename_minors_impl(const char *oldname, const char *newname)
1369 {
1370 zvol_state_t *zv, *zv_next;
1371 int oldnamelen;
1372
1373 if (zvol_inhibit_dev)
1374 return;
1375
1376 oldnamelen = strlen(oldname);
1377
1378 rw_enter(&zvol_state_lock, RW_READER);
1379
1380 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1381 zv_next = list_next(&zvol_state_list, zv);
1382
1383 mutex_enter(&zv->zv_state_lock);
1384
1385 if (strcmp(zv->zv_name, oldname) == 0) {
1386 zvol_os_rename_minor(zv, newname);
1387 } else if (strncmp(zv->zv_name, oldname, oldnamelen) == 0 &&
1388 (zv->zv_name[oldnamelen] == '/' ||
1389 zv->zv_name[oldnamelen] == '@')) {
1390 char *name = kmem_asprintf("%s%c%s", newname,
1391 zv->zv_name[oldnamelen],
1392 zv->zv_name + oldnamelen + 1);
1393 zvol_os_rename_minor(zv, name);
1394 kmem_strfree(name);
1395 }
1396
1397 mutex_exit(&zv->zv_state_lock);
1398 }
1399
1400 rw_exit(&zvol_state_lock);
1401 }
1402
1403 typedef struct zvol_snapdev_cb_arg {
1404 uint64_t snapdev;
1405 } zvol_snapdev_cb_arg_t;
1406
1407 static int
1408 zvol_set_snapdev_cb(const char *dsname, void *param)
1409 {
1410 zvol_snapdev_cb_arg_t *arg = param;
1411
1412 if (strchr(dsname, '@') == NULL)
1413 return (0);
1414
1415 switch (arg->snapdev) {
1416 case ZFS_SNAPDEV_VISIBLE:
1417 (void) zvol_os_create_minor(dsname);
1418 break;
1419 case ZFS_SNAPDEV_HIDDEN:
1420 (void) zvol_remove_minor_impl(dsname);
1421 break;
1422 }
1423
1424 return (0);
1425 }
1426
1427 static void
1428 zvol_set_snapdev_impl(char *name, uint64_t snapdev)
1429 {
1430 zvol_snapdev_cb_arg_t arg = {snapdev};
1431 fstrans_cookie_t cookie = spl_fstrans_mark();
1432 /*
1433 * The zvol_set_snapdev_sync() sets snapdev appropriately
1434 * in the dataset hierarchy. Here, we only scan snapshots.
1435 */
1436 dmu_objset_find(name, zvol_set_snapdev_cb, &arg, DS_FIND_SNAPSHOTS);
1437 spl_fstrans_unmark(cookie);
1438 }
1439
1440 static void
1441 zvol_set_volmode_impl(char *name, uint64_t volmode)
1442 {
1443 fstrans_cookie_t cookie;
1444 uint64_t old_volmode;
1445 zvol_state_t *zv;
1446
1447 if (strchr(name, '@') != NULL)
1448 return;
1449
1450 /*
1451 * It's unfortunate we need to remove minors before we create new ones:
1452 * this is necessary because our backing gendisk (zvol_state->zv_disk)
1453 * could be different when we set, for instance, volmode from "geom"
1454 * to "dev" (or vice versa).
1455 */
1456 zv = zvol_find_by_name(name, RW_NONE);
1457 if (zv == NULL && volmode == ZFS_VOLMODE_NONE)
1458 return;
1459 if (zv != NULL) {
1460 old_volmode = zv->zv_volmode;
1461 mutex_exit(&zv->zv_state_lock);
1462 if (old_volmode == volmode)
1463 return;
1464 zvol_wait_close(zv);
1465 }
1466 cookie = spl_fstrans_mark();
1467 switch (volmode) {
1468 case ZFS_VOLMODE_NONE:
1469 (void) zvol_remove_minor_impl(name);
1470 break;
1471 case ZFS_VOLMODE_GEOM:
1472 case ZFS_VOLMODE_DEV:
1473 (void) zvol_remove_minor_impl(name);
1474 (void) zvol_os_create_minor(name);
1475 break;
1476 case ZFS_VOLMODE_DEFAULT:
1477 (void) zvol_remove_minor_impl(name);
1478 if (zvol_volmode == ZFS_VOLMODE_NONE)
1479 break;
1480 else /* if zvol_volmode is invalid defaults to "geom" */
1481 (void) zvol_os_create_minor(name);
1482 break;
1483 }
1484 spl_fstrans_unmark(cookie);
1485 }
1486
1487 static zvol_task_t *
1488 zvol_task_alloc(zvol_async_op_t op, const char *name1, const char *name2,
1489 uint64_t value)
1490 {
1491 zvol_task_t *task;
1492
1493 /* Never allow tasks on hidden names. */
1494 if (name1[0] == '$')
1495 return (NULL);
1496
1497 task = kmem_zalloc(sizeof (zvol_task_t), KM_SLEEP);
1498 task->op = op;
1499 task->value = value;
1500
1501 strlcpy(task->name1, name1, MAXNAMELEN);
1502 if (name2 != NULL)
1503 strlcpy(task->name2, name2, MAXNAMELEN);
1504
1505 return (task);
1506 }
1507
1508 static void
1509 zvol_task_free(zvol_task_t *task)
1510 {
1511 kmem_free(task, sizeof (zvol_task_t));
1512 }
1513
1514 /*
1515 * The worker thread function performed asynchronously.
1516 */
1517 static void
1518 zvol_task_cb(void *arg)
1519 {
1520 zvol_task_t *task = arg;
1521
1522 switch (task->op) {
1523 case ZVOL_ASYNC_REMOVE_MINORS:
1524 zvol_remove_minors_impl(task->name1);
1525 break;
1526 case ZVOL_ASYNC_RENAME_MINORS:
1527 zvol_rename_minors_impl(task->name1, task->name2);
1528 break;
1529 case ZVOL_ASYNC_SET_SNAPDEV:
1530 zvol_set_snapdev_impl(task->name1, task->value);
1531 break;
1532 case ZVOL_ASYNC_SET_VOLMODE:
1533 zvol_set_volmode_impl(task->name1, task->value);
1534 break;
1535 default:
1536 VERIFY(0);
1537 break;
1538 }
1539
1540 zvol_task_free(task);
1541 }
1542
1543 typedef struct zvol_set_prop_int_arg {
1544 const char *zsda_name;
1545 uint64_t zsda_value;
1546 zprop_source_t zsda_source;
1547 dmu_tx_t *zsda_tx;
1548 } zvol_set_prop_int_arg_t;
1549
1550 /*
1551 * Sanity check the dataset for safe use by the sync task. No additional
1552 * conditions are imposed.
1553 */
1554 static int
1555 zvol_set_snapdev_check(void *arg, dmu_tx_t *tx)
1556 {
1557 zvol_set_prop_int_arg_t *zsda = arg;
1558 dsl_pool_t *dp = dmu_tx_pool(tx);
1559 dsl_dir_t *dd;
1560 int error;
1561
1562 error = dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL);
1563 if (error != 0)
1564 return (error);
1565
1566 dsl_dir_rele(dd, FTAG);
1567
1568 return (error);
1569 }
1570
1571 static int
1572 zvol_set_snapdev_sync_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
1573 {
1574 (void) arg;
1575 char dsname[MAXNAMELEN];
1576 zvol_task_t *task;
1577 uint64_t snapdev;
1578
1579 dsl_dataset_name(ds, dsname);
1580 if (dsl_prop_get_int_ds(ds, "snapdev", &snapdev) != 0)
1581 return (0);
1582 task = zvol_task_alloc(ZVOL_ASYNC_SET_SNAPDEV, dsname, NULL, snapdev);
1583 if (task == NULL)
1584 return (0);
1585
1586 (void) taskq_dispatch(dp->dp_spa->spa_zvol_taskq, zvol_task_cb,
1587 task, TQ_SLEEP);
1588 return (0);
1589 }
1590
1591 /*
1592 * Traverse all child datasets and apply snapdev appropriately.
1593 * We call dsl_prop_set_sync_impl() here to set the value only on the toplevel
1594 * dataset and read the effective "snapdev" on every child in the callback
1595 * function: this is because the value is not guaranteed to be the same in the
1596 * whole dataset hierarchy.
1597 */
1598 static void
1599 zvol_set_snapdev_sync(void *arg, dmu_tx_t *tx)
1600 {
1601 zvol_set_prop_int_arg_t *zsda = arg;
1602 dsl_pool_t *dp = dmu_tx_pool(tx);
1603 dsl_dir_t *dd;
1604 dsl_dataset_t *ds;
1605 int error;
1606
1607 VERIFY0(dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL));
1608 zsda->zsda_tx = tx;
1609
1610 error = dsl_dataset_hold(dp, zsda->zsda_name, FTAG, &ds);
1611 if (error == 0) {
1612 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_SNAPDEV),
1613 zsda->zsda_source, sizeof (zsda->zsda_value), 1,
1614 &zsda->zsda_value, zsda->zsda_tx);
1615 dsl_dataset_rele(ds, FTAG);
1616 }
1617 dmu_objset_find_dp(dp, dd->dd_object, zvol_set_snapdev_sync_cb,
1618 zsda, DS_FIND_CHILDREN);
1619
1620 dsl_dir_rele(dd, FTAG);
1621 }
1622
1623 int
1624 zvol_set_snapdev(const char *ddname, zprop_source_t source, uint64_t snapdev)
1625 {
1626 zvol_set_prop_int_arg_t zsda;
1627
1628 zsda.zsda_name = ddname;
1629 zsda.zsda_source = source;
1630 zsda.zsda_value = snapdev;
1631
1632 return (dsl_sync_task(ddname, zvol_set_snapdev_check,
1633 zvol_set_snapdev_sync, &zsda, 0, ZFS_SPACE_CHECK_NONE));
1634 }
1635
1636 /*
1637 * Sanity check the dataset for safe use by the sync task. No additional
1638 * conditions are imposed.
1639 */
1640 static int
1641 zvol_set_volmode_check(void *arg, dmu_tx_t *tx)
1642 {
1643 zvol_set_prop_int_arg_t *zsda = arg;
1644 dsl_pool_t *dp = dmu_tx_pool(tx);
1645 dsl_dir_t *dd;
1646 int error;
1647
1648 error = dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL);
1649 if (error != 0)
1650 return (error);
1651
1652 dsl_dir_rele(dd, FTAG);
1653
1654 return (error);
1655 }
1656
1657 static int
1658 zvol_set_volmode_sync_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
1659 {
1660 (void) arg;
1661 char dsname[MAXNAMELEN];
1662 zvol_task_t *task;
1663 uint64_t volmode;
1664
1665 dsl_dataset_name(ds, dsname);
1666 if (dsl_prop_get_int_ds(ds, "volmode", &volmode) != 0)
1667 return (0);
1668 task = zvol_task_alloc(ZVOL_ASYNC_SET_VOLMODE, dsname, NULL, volmode);
1669 if (task == NULL)
1670 return (0);
1671
1672 (void) taskq_dispatch(dp->dp_spa->spa_zvol_taskq, zvol_task_cb,
1673 task, TQ_SLEEP);
1674 return (0);
1675 }
1676
1677 /*
1678 * Traverse all child datasets and apply volmode appropriately.
1679 * We call dsl_prop_set_sync_impl() here to set the value only on the toplevel
1680 * dataset and read the effective "volmode" on every child in the callback
1681 * function: this is because the value is not guaranteed to be the same in the
1682 * whole dataset hierarchy.
1683 */
1684 static void
1685 zvol_set_volmode_sync(void *arg, dmu_tx_t *tx)
1686 {
1687 zvol_set_prop_int_arg_t *zsda = arg;
1688 dsl_pool_t *dp = dmu_tx_pool(tx);
1689 dsl_dir_t *dd;
1690 dsl_dataset_t *ds;
1691 int error;
1692
1693 VERIFY0(dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL));
1694 zsda->zsda_tx = tx;
1695
1696 error = dsl_dataset_hold(dp, zsda->zsda_name, FTAG, &ds);
1697 if (error == 0) {
1698 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_VOLMODE),
1699 zsda->zsda_source, sizeof (zsda->zsda_value), 1,
1700 &zsda->zsda_value, zsda->zsda_tx);
1701 dsl_dataset_rele(ds, FTAG);
1702 }
1703
1704 dmu_objset_find_dp(dp, dd->dd_object, zvol_set_volmode_sync_cb,
1705 zsda, DS_FIND_CHILDREN);
1706
1707 dsl_dir_rele(dd, FTAG);
1708 }
1709
1710 int
1711 zvol_set_volmode(const char *ddname, zprop_source_t source, uint64_t volmode)
1712 {
1713 zvol_set_prop_int_arg_t zsda;
1714
1715 zsda.zsda_name = ddname;
1716 zsda.zsda_source = source;
1717 zsda.zsda_value = volmode;
1718
1719 return (dsl_sync_task(ddname, zvol_set_volmode_check,
1720 zvol_set_volmode_sync, &zsda, 0, ZFS_SPACE_CHECK_NONE));
1721 }
1722
1723 void
1724 zvol_remove_minors(spa_t *spa, const char *name, boolean_t async)
1725 {
1726 zvol_task_t *task;
1727 taskqid_t id;
1728
1729 task = zvol_task_alloc(ZVOL_ASYNC_REMOVE_MINORS, name, NULL, ~0ULL);
1730 if (task == NULL)
1731 return;
1732
1733 id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP);
1734 if ((async == B_FALSE) && (id != TASKQID_INVALID))
1735 taskq_wait_id(spa->spa_zvol_taskq, id);
1736 }
1737
1738 void
1739 zvol_rename_minors(spa_t *spa, const char *name1, const char *name2,
1740 boolean_t async)
1741 {
1742 zvol_task_t *task;
1743 taskqid_t id;
1744
1745 task = zvol_task_alloc(ZVOL_ASYNC_RENAME_MINORS, name1, name2, ~0ULL);
1746 if (task == NULL)
1747 return;
1748
1749 id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP);
1750 if ((async == B_FALSE) && (id != TASKQID_INVALID))
1751 taskq_wait_id(spa->spa_zvol_taskq, id);
1752 }
1753
1754 boolean_t
1755 zvol_is_zvol(const char *name)
1756 {
1757
1758 return (zvol_os_is_zvol(name));
1759 }
1760
1761 int
1762 zvol_init_impl(void)
1763 {
1764 int i;
1765
1766 list_create(&zvol_state_list, sizeof (zvol_state_t),
1767 offsetof(zvol_state_t, zv_next));
1768 rw_init(&zvol_state_lock, NULL, RW_DEFAULT, NULL);
1769
1770 zvol_htable = kmem_alloc(ZVOL_HT_SIZE * sizeof (struct hlist_head),
1771 KM_SLEEP);
1772 for (i = 0; i < ZVOL_HT_SIZE; i++)
1773 INIT_HLIST_HEAD(&zvol_htable[i]);
1774
1775 return (0);
1776 }
1777
1778 void
1779 zvol_fini_impl(void)
1780 {
1781 zvol_remove_minors_impl(NULL);
1782
1783 /*
1784 * The call to "zvol_remove_minors_impl" may dispatch entries to
1785 * the system_taskq, but it doesn't wait for those entries to
1786 * complete before it returns. Thus, we must wait for all of the
1787 * removals to finish, before we can continue.
1788 */
1789 taskq_wait_outstanding(system_taskq, 0);
1790
1791 kmem_free(zvol_htable, ZVOL_HT_SIZE * sizeof (struct hlist_head));
1792 list_destroy(&zvol_state_list);
1793 rw_destroy(&zvol_state_lock);
1794 }