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 <[email protected]>.
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
zvol_name_hash(const char * name)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 *
zvol_find_by_name_hash(const char * name,uint64_t hash,int mode)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 *
zvol_find_by_name(const char * name,int mode)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
zvol_create_cb(objset_t * os,void * arg,cred_t * cr,dmu_tx_t * tx)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
zvol_get_stats(objset_t * os,nvlist_t * nv)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
zvol_check_volsize(uint64_t volsize,uint64_t blocksize)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
zvol_update_volsize(uint64_t volsize,objset_t * os)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
zvol_set_volsize(const char * name,uint64_t volsize)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
zvol_check_volblocksize(const char * name,uint64_t volblocksize)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
zvol_replay_truncate(void * arg1,void * arg2,boolean_t byteswap)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 ASSERT3U(lr->lr_common.lrc_reclen, >=, sizeof (*lr));
421
422 if (byteswap)
423 byteswap_uint64_array(lr, sizeof (*lr));
424
425 offset = lr->lr_offset;
426 length = lr->lr_length;
427
428 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
429 dmu_tx_mark_netfree(tx);
430 int error = dmu_tx_assign(tx, TXG_WAIT);
431 if (error != 0) {
432 dmu_tx_abort(tx);
433 } else {
434 (void) zil_replaying(zv->zv_zilog, tx);
435 dmu_tx_commit(tx);
436 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, offset,
437 length);
438 }
439
440 return (error);
441 }
442
443 /*
444 * Replay a TX_WRITE ZIL transaction that didn't get committed
445 * after a system failure
446 */
447 static int
zvol_replay_write(void * arg1,void * arg2,boolean_t byteswap)448 zvol_replay_write(void *arg1, void *arg2, boolean_t byteswap)
449 {
450 zvol_state_t *zv = arg1;
451 lr_write_t *lr = arg2;
452 objset_t *os = zv->zv_objset;
453 char *data = (char *)(lr + 1); /* data follows lr_write_t */
454 uint64_t offset, length;
455 dmu_tx_t *tx;
456 int error;
457
458 ASSERT3U(lr->lr_common.lrc_reclen, >=, sizeof (*lr));
459
460 if (byteswap)
461 byteswap_uint64_array(lr, sizeof (*lr));
462
463 offset = lr->lr_offset;
464 length = lr->lr_length;
465
466 /* If it's a dmu_sync() block, write the whole block */
467 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
468 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
469 if (length < blocksize) {
470 offset -= offset % blocksize;
471 length = blocksize;
472 }
473 }
474
475 tx = dmu_tx_create(os);
476 dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length);
477 error = dmu_tx_assign(tx, TXG_WAIT);
478 if (error) {
479 dmu_tx_abort(tx);
480 } else {
481 dmu_write(os, ZVOL_OBJ, offset, length, data, tx);
482 (void) zil_replaying(zv->zv_zilog, tx);
483 dmu_tx_commit(tx);
484 }
485
486 return (error);
487 }
488
489 static int
zvol_replay_err(void * arg1,void * arg2,boolean_t byteswap)490 zvol_replay_err(void *arg1, void *arg2, boolean_t byteswap)
491 {
492 (void) arg1, (void) arg2, (void) byteswap;
493 return (SET_ERROR(ENOTSUP));
494 }
495
496 /*
497 * Callback vectors for replaying records.
498 * Only TX_WRITE and TX_TRUNCATE are needed for zvol.
499 */
500 zil_replay_func_t *const zvol_replay_vector[TX_MAX_TYPE] = {
501 zvol_replay_err, /* no such transaction type */
502 zvol_replay_err, /* TX_CREATE */
503 zvol_replay_err, /* TX_MKDIR */
504 zvol_replay_err, /* TX_MKXATTR */
505 zvol_replay_err, /* TX_SYMLINK */
506 zvol_replay_err, /* TX_REMOVE */
507 zvol_replay_err, /* TX_RMDIR */
508 zvol_replay_err, /* TX_LINK */
509 zvol_replay_err, /* TX_RENAME */
510 zvol_replay_write, /* TX_WRITE */
511 zvol_replay_truncate, /* TX_TRUNCATE */
512 zvol_replay_err, /* TX_SETATTR */
513 zvol_replay_err, /* TX_ACL */
514 zvol_replay_err, /* TX_CREATE_ATTR */
515 zvol_replay_err, /* TX_CREATE_ACL_ATTR */
516 zvol_replay_err, /* TX_MKDIR_ACL */
517 zvol_replay_err, /* TX_MKDIR_ATTR */
518 zvol_replay_err, /* TX_MKDIR_ACL_ATTR */
519 zvol_replay_err, /* TX_WRITE2 */
520 zvol_replay_err, /* TX_SETSAXATTR */
521 zvol_replay_err, /* TX_RENAME_EXCHANGE */
522 zvol_replay_err, /* TX_RENAME_WHITEOUT */
523 zvol_replay_err, /* TX_CLONE_RANGE */
524 };
525
526 /*
527 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
528 *
529 * We store data in the log buffers if it's small enough.
530 * Otherwise we will later flush the data out via dmu_sync().
531 */
532 static const ssize_t zvol_immediate_write_sz = 32768;
533
534 void
zvol_log_write(zvol_state_t * zv,dmu_tx_t * tx,uint64_t offset,uint64_t size,int sync)535 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, uint64_t offset,
536 uint64_t size, int sync)
537 {
538 uint32_t blocksize = zv->zv_volblocksize;
539 zilog_t *zilog = zv->zv_zilog;
540 itx_wr_state_t write_state;
541 uint64_t sz = size;
542
543 if (zil_replaying(zilog, tx))
544 return;
545
546 if (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
547 write_state = WR_INDIRECT;
548 else if (!spa_has_slogs(zilog->zl_spa) &&
549 size >= blocksize && blocksize > zvol_immediate_write_sz)
550 write_state = WR_INDIRECT;
551 else if (sync)
552 write_state = WR_COPIED;
553 else
554 write_state = WR_NEED_COPY;
555
556 while (size) {
557 itx_t *itx;
558 lr_write_t *lr;
559 itx_wr_state_t wr_state = write_state;
560 ssize_t len = size;
561
562 if (wr_state == WR_COPIED && size > zil_max_copied_data(zilog))
563 wr_state = WR_NEED_COPY;
564 else if (wr_state == WR_INDIRECT)
565 len = MIN(blocksize - P2PHASE(offset, blocksize), size);
566
567 itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
568 (wr_state == WR_COPIED ? len : 0));
569 lr = (lr_write_t *)&itx->itx_lr;
570 if (wr_state == WR_COPIED && dmu_read_by_dnode(zv->zv_dn,
571 offset, len, lr+1, DMU_READ_NO_PREFETCH) != 0) {
572 zil_itx_destroy(itx);
573 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
574 lr = (lr_write_t *)&itx->itx_lr;
575 wr_state = WR_NEED_COPY;
576 }
577
578 itx->itx_wr_state = wr_state;
579 lr->lr_foid = ZVOL_OBJ;
580 lr->lr_offset = offset;
581 lr->lr_length = len;
582 lr->lr_blkoff = 0;
583 BP_ZERO(&lr->lr_blkptr);
584
585 itx->itx_private = zv;
586 itx->itx_sync = sync;
587
588 (void) zil_itx_assign(zilog, itx, tx);
589
590 offset += len;
591 size -= len;
592 }
593
594 if (write_state == WR_COPIED || write_state == WR_NEED_COPY) {
595 dsl_pool_wrlog_count(zilog->zl_dmu_pool, sz, tx->tx_txg);
596 }
597 }
598
599 /*
600 * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE.
601 */
602 void
zvol_log_truncate(zvol_state_t * zv,dmu_tx_t * tx,uint64_t off,uint64_t len,boolean_t sync)603 zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off, uint64_t len,
604 boolean_t sync)
605 {
606 itx_t *itx;
607 lr_truncate_t *lr;
608 zilog_t *zilog = zv->zv_zilog;
609
610 if (zil_replaying(zilog, tx))
611 return;
612
613 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
614 lr = (lr_truncate_t *)&itx->itx_lr;
615 lr->lr_foid = ZVOL_OBJ;
616 lr->lr_offset = off;
617 lr->lr_length = len;
618
619 itx->itx_sync = sync;
620 zil_itx_assign(zilog, itx, tx);
621 }
622
623
624 static void
zvol_get_done(zgd_t * zgd,int error)625 zvol_get_done(zgd_t *zgd, int error)
626 {
627 (void) error;
628 if (zgd->zgd_db)
629 dmu_buf_rele(zgd->zgd_db, zgd);
630
631 zfs_rangelock_exit(zgd->zgd_lr);
632
633 kmem_free(zgd, sizeof (zgd_t));
634 }
635
636 /*
637 * Get data to generate a TX_WRITE intent log record.
638 */
639 int
zvol_get_data(void * arg,uint64_t arg2,lr_write_t * lr,char * buf,struct lwb * lwb,zio_t * zio)640 zvol_get_data(void *arg, uint64_t arg2, lr_write_t *lr, char *buf,
641 struct lwb *lwb, zio_t *zio)
642 {
643 zvol_state_t *zv = arg;
644 uint64_t offset = lr->lr_offset;
645 uint64_t size = lr->lr_length;
646 dmu_buf_t *db;
647 zgd_t *zgd;
648 int error;
649
650 ASSERT3P(lwb, !=, NULL);
651 ASSERT3U(size, !=, 0);
652
653 zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
654 zgd->zgd_lwb = lwb;
655
656 /*
657 * Write records come in two flavors: immediate and indirect.
658 * For small writes it's cheaper to store the data with the
659 * log record (immediate); for large writes it's cheaper to
660 * sync the data and get a pointer to it (indirect) so that
661 * we don't have to write the data twice.
662 */
663 if (buf != NULL) { /* immediate write */
664 zgd->zgd_lr = zfs_rangelock_enter(&zv->zv_rangelock, offset,
665 size, RL_READER);
666 error = dmu_read_by_dnode(zv->zv_dn, offset, size, buf,
667 DMU_READ_NO_PREFETCH);
668 } else { /* indirect write */
669 ASSERT3P(zio, !=, NULL);
670 /*
671 * Have to lock the whole block to ensure when it's written out
672 * and its checksum is being calculated that no one can change
673 * the data. Contrarily to zfs_get_data we need not re-check
674 * blocksize after we get the lock because it cannot be changed.
675 */
676 size = zv->zv_volblocksize;
677 offset = P2ALIGN_TYPED(offset, size, uint64_t);
678 zgd->zgd_lr = zfs_rangelock_enter(&zv->zv_rangelock, offset,
679 size, RL_READER);
680 error = dmu_buf_hold_noread_by_dnode(zv->zv_dn, offset, zgd,
681 &db);
682 if (error == 0) {
683 blkptr_t *bp = &lr->lr_blkptr;
684
685 zgd->zgd_db = db;
686 zgd->zgd_bp = bp;
687
688 ASSERT(db != NULL);
689 ASSERT(db->db_offset == offset);
690 ASSERT(db->db_size == size);
691
692 error = dmu_sync(zio, lr->lr_common.lrc_txg,
693 zvol_get_done, zgd);
694
695 if (error == 0)
696 return (0);
697 }
698 }
699
700 zvol_get_done(zgd, error);
701
702 return (SET_ERROR(error));
703 }
704
705 /*
706 * The zvol_state_t's are inserted into zvol_state_list and zvol_htable.
707 */
708
709 void
zvol_insert(zvol_state_t * zv)710 zvol_insert(zvol_state_t *zv)
711 {
712 ASSERT(RW_WRITE_HELD(&zvol_state_lock));
713 list_insert_head(&zvol_state_list, zv);
714 hlist_add_head(&zv->zv_hlink, ZVOL_HT_HEAD(zv->zv_hash));
715 }
716
717 /*
718 * Simply remove the zvol from to list of zvols.
719 */
720 static void
zvol_remove(zvol_state_t * zv)721 zvol_remove(zvol_state_t *zv)
722 {
723 ASSERT(RW_WRITE_HELD(&zvol_state_lock));
724 list_remove(&zvol_state_list, zv);
725 hlist_del(&zv->zv_hlink);
726 }
727
728 /*
729 * Setup zv after we just own the zv->objset
730 */
731 static int
zvol_setup_zv(zvol_state_t * zv)732 zvol_setup_zv(zvol_state_t *zv)
733 {
734 uint64_t volsize;
735 int error;
736 uint64_t ro;
737 objset_t *os = zv->zv_objset;
738
739 ASSERT(MUTEX_HELD(&zv->zv_state_lock));
740 ASSERT(RW_LOCK_HELD(&zv->zv_suspend_lock));
741
742 zv->zv_zilog = NULL;
743 zv->zv_flags &= ~ZVOL_WRITTEN_TO;
744
745 error = dsl_prop_get_integer(zv->zv_name, "readonly", &ro, NULL);
746 if (error)
747 return (SET_ERROR(error));
748
749 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
750 if (error)
751 return (SET_ERROR(error));
752
753 error = dnode_hold(os, ZVOL_OBJ, zv, &zv->zv_dn);
754 if (error)
755 return (SET_ERROR(error));
756
757 zvol_os_set_capacity(zv, volsize >> 9);
758 zv->zv_volsize = volsize;
759
760 if (ro || dmu_objset_is_snapshot(os) ||
761 !spa_writeable(dmu_objset_spa(os))) {
762 zvol_os_set_disk_ro(zv, 1);
763 zv->zv_flags |= ZVOL_RDONLY;
764 } else {
765 zvol_os_set_disk_ro(zv, 0);
766 zv->zv_flags &= ~ZVOL_RDONLY;
767 }
768 return (0);
769 }
770
771 /*
772 * Shutdown every zv_objset related stuff except zv_objset itself.
773 * The is the reverse of zvol_setup_zv.
774 */
775 static void
zvol_shutdown_zv(zvol_state_t * zv)776 zvol_shutdown_zv(zvol_state_t *zv)
777 {
778 ASSERT(MUTEX_HELD(&zv->zv_state_lock) &&
779 RW_LOCK_HELD(&zv->zv_suspend_lock));
780
781 if (zv->zv_flags & ZVOL_WRITTEN_TO) {
782 ASSERT(zv->zv_zilog != NULL);
783 zil_close(zv->zv_zilog);
784 }
785
786 zv->zv_zilog = NULL;
787
788 dnode_rele(zv->zv_dn, zv);
789 zv->zv_dn = NULL;
790
791 /*
792 * Evict cached data. We must write out any dirty data before
793 * disowning the dataset.
794 */
795 if (zv->zv_flags & ZVOL_WRITTEN_TO)
796 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
797 (void) dmu_objset_evict_dbufs(zv->zv_objset);
798 }
799
800 /*
801 * return the proper tag for rollback and recv
802 */
803 void *
zvol_tag(zvol_state_t * zv)804 zvol_tag(zvol_state_t *zv)
805 {
806 ASSERT(RW_WRITE_HELD(&zv->zv_suspend_lock));
807 return (zv->zv_open_count > 0 ? zv : NULL);
808 }
809
810 /*
811 * Suspend the zvol for recv and rollback.
812 */
813 zvol_state_t *
zvol_suspend(const char * name)814 zvol_suspend(const char *name)
815 {
816 zvol_state_t *zv;
817
818 zv = zvol_find_by_name(name, RW_WRITER);
819
820 if (zv == NULL)
821 return (NULL);
822
823 /* block all I/O, release in zvol_resume. */
824 ASSERT(MUTEX_HELD(&zv->zv_state_lock));
825 ASSERT(RW_WRITE_HELD(&zv->zv_suspend_lock));
826
827 atomic_inc(&zv->zv_suspend_ref);
828
829 if (zv->zv_open_count > 0)
830 zvol_shutdown_zv(zv);
831
832 /*
833 * do not hold zv_state_lock across suspend/resume to
834 * avoid locking up zvol lookups
835 */
836 mutex_exit(&zv->zv_state_lock);
837
838 /* zv_suspend_lock is released in zvol_resume() */
839 return (zv);
840 }
841
842 int
zvol_resume(zvol_state_t * zv)843 zvol_resume(zvol_state_t *zv)
844 {
845 int error = 0;
846
847 ASSERT(RW_WRITE_HELD(&zv->zv_suspend_lock));
848
849 mutex_enter(&zv->zv_state_lock);
850
851 if (zv->zv_open_count > 0) {
852 VERIFY0(dmu_objset_hold(zv->zv_name, zv, &zv->zv_objset));
853 VERIFY3P(zv->zv_objset->os_dsl_dataset->ds_owner, ==, zv);
854 VERIFY(dsl_dataset_long_held(zv->zv_objset->os_dsl_dataset));
855 dmu_objset_rele(zv->zv_objset, zv);
856
857 error = zvol_setup_zv(zv);
858 }
859
860 mutex_exit(&zv->zv_state_lock);
861
862 rw_exit(&zv->zv_suspend_lock);
863 /*
864 * We need this because we don't hold zvol_state_lock while releasing
865 * zv_suspend_lock. zvol_remove_minors_impl thus cannot check
866 * zv_suspend_lock to determine it is safe to free because rwlock is
867 * not inherent atomic.
868 */
869 atomic_dec(&zv->zv_suspend_ref);
870
871 return (SET_ERROR(error));
872 }
873
874 int
zvol_first_open(zvol_state_t * zv,boolean_t readonly)875 zvol_first_open(zvol_state_t *zv, boolean_t readonly)
876 {
877 objset_t *os;
878 int error;
879
880 ASSERT(RW_READ_HELD(&zv->zv_suspend_lock));
881 ASSERT(MUTEX_HELD(&zv->zv_state_lock));
882 ASSERT(mutex_owned(&spa_namespace_lock));
883
884 boolean_t ro = (readonly || (strchr(zv->zv_name, '@') != NULL));
885 error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, ro, B_TRUE, zv, &os);
886 if (error)
887 return (SET_ERROR(error));
888
889 zv->zv_objset = os;
890
891 error = zvol_setup_zv(zv);
892 if (error) {
893 dmu_objset_disown(os, 1, zv);
894 zv->zv_objset = NULL;
895 }
896
897 return (error);
898 }
899
900 void
zvol_last_close(zvol_state_t * zv)901 zvol_last_close(zvol_state_t *zv)
902 {
903 ASSERT(RW_READ_HELD(&zv->zv_suspend_lock));
904 ASSERT(MUTEX_HELD(&zv->zv_state_lock));
905
906 zvol_shutdown_zv(zv);
907
908 dmu_objset_disown(zv->zv_objset, 1, zv);
909 zv->zv_objset = NULL;
910 }
911
912 typedef struct minors_job {
913 list_t *list;
914 list_node_t link;
915 /* input */
916 char *name;
917 /* output */
918 int error;
919 } minors_job_t;
920
921 /*
922 * Prefetch zvol dnodes for the minors_job
923 */
924 static void
zvol_prefetch_minors_impl(void * arg)925 zvol_prefetch_minors_impl(void *arg)
926 {
927 minors_job_t *job = arg;
928 char *dsname = job->name;
929 objset_t *os = NULL;
930
931 job->error = dmu_objset_own(dsname, DMU_OST_ZVOL, B_TRUE, B_TRUE,
932 FTAG, &os);
933 if (job->error == 0) {
934 dmu_prefetch_dnode(os, ZVOL_OBJ, ZIO_PRIORITY_SYNC_READ);
935 dmu_objset_disown(os, B_TRUE, FTAG);
936 }
937 }
938
939 /*
940 * Mask errors to continue dmu_objset_find() traversal
941 */
942 static int
zvol_create_snap_minor_cb(const char * dsname,void * arg)943 zvol_create_snap_minor_cb(const char *dsname, void *arg)
944 {
945 minors_job_t *j = arg;
946 list_t *minors_list = j->list;
947 const char *name = j->name;
948
949 ASSERT0(MUTEX_HELD(&spa_namespace_lock));
950
951 /* skip the designated dataset */
952 if (name && strcmp(dsname, name) == 0)
953 return (0);
954
955 /* at this point, the dsname should name a snapshot */
956 if (strchr(dsname, '@') == 0) {
957 dprintf("zvol_create_snap_minor_cb(): "
958 "%s is not a snapshot name\n", dsname);
959 } else {
960 minors_job_t *job;
961 char *n = kmem_strdup(dsname);
962 if (n == NULL)
963 return (0);
964
965 job = kmem_alloc(sizeof (minors_job_t), KM_SLEEP);
966 job->name = n;
967 job->list = minors_list;
968 job->error = 0;
969 list_insert_tail(minors_list, job);
970 /* don't care if dispatch fails, because job->error is 0 */
971 taskq_dispatch(system_taskq, zvol_prefetch_minors_impl, job,
972 TQ_SLEEP);
973 }
974
975 return (0);
976 }
977
978 /*
979 * If spa_keystore_load_wkey() is called for an encrypted zvol,
980 * we need to look for any clones also using the key. This function
981 * is "best effort" - so we just skip over it if there are failures.
982 */
983 static void
zvol_add_clones(const char * dsname,list_t * minors_list)984 zvol_add_clones(const char *dsname, list_t *minors_list)
985 {
986 /* Also check if it has clones */
987 dsl_dir_t *dd = NULL;
988 dsl_pool_t *dp = NULL;
989
990 if (dsl_pool_hold(dsname, FTAG, &dp) != 0)
991 return;
992
993 if (!spa_feature_is_enabled(dp->dp_spa,
994 SPA_FEATURE_ENCRYPTION))
995 goto out;
996
997 if (dsl_dir_hold(dp, dsname, FTAG, &dd, NULL) != 0)
998 goto out;
999
1000 if (dsl_dir_phys(dd)->dd_clones == 0)
1001 goto out;
1002
1003 zap_cursor_t *zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP);
1004 zap_attribute_t *za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1005 objset_t *mos = dd->dd_pool->dp_meta_objset;
1006
1007 for (zap_cursor_init(zc, mos, dsl_dir_phys(dd)->dd_clones);
1008 zap_cursor_retrieve(zc, za) == 0;
1009 zap_cursor_advance(zc)) {
1010 dsl_dataset_t *clone;
1011 minors_job_t *job;
1012
1013 if (dsl_dataset_hold_obj(dd->dd_pool,
1014 za->za_first_integer, FTAG, &clone) == 0) {
1015
1016 char name[ZFS_MAX_DATASET_NAME_LEN];
1017 dsl_dataset_name(clone, name);
1018
1019 char *n = kmem_strdup(name);
1020 job = kmem_alloc(sizeof (minors_job_t), KM_SLEEP);
1021 job->name = n;
1022 job->list = minors_list;
1023 job->error = 0;
1024 list_insert_tail(minors_list, job);
1025
1026 dsl_dataset_rele(clone, FTAG);
1027 }
1028 }
1029 zap_cursor_fini(zc);
1030 kmem_free(za, sizeof (zap_attribute_t));
1031 kmem_free(zc, sizeof (zap_cursor_t));
1032
1033 out:
1034 if (dd != NULL)
1035 dsl_dir_rele(dd, FTAG);
1036 dsl_pool_rele(dp, FTAG);
1037 }
1038
1039 /*
1040 * Mask errors to continue dmu_objset_find() traversal
1041 */
1042 static int
zvol_create_minors_cb(const char * dsname,void * arg)1043 zvol_create_minors_cb(const char *dsname, void *arg)
1044 {
1045 uint64_t snapdev;
1046 int error;
1047 list_t *minors_list = arg;
1048
1049 ASSERT0(MUTEX_HELD(&spa_namespace_lock));
1050
1051 error = dsl_prop_get_integer(dsname, "snapdev", &snapdev, NULL);
1052 if (error)
1053 return (0);
1054
1055 /*
1056 * Given the name and the 'snapdev' property, create device minor nodes
1057 * with the linkages to zvols/snapshots as needed.
1058 * If the name represents a zvol, create a minor node for the zvol, then
1059 * check if its snapshots are 'visible', and if so, iterate over the
1060 * snapshots and create device minor nodes for those.
1061 */
1062 if (strchr(dsname, '@') == 0) {
1063 minors_job_t *job;
1064 char *n = kmem_strdup(dsname);
1065 if (n == NULL)
1066 return (0);
1067
1068 job = kmem_alloc(sizeof (minors_job_t), KM_SLEEP);
1069 job->name = n;
1070 job->list = minors_list;
1071 job->error = 0;
1072 list_insert_tail(minors_list, job);
1073 /* don't care if dispatch fails, because job->error is 0 */
1074 taskq_dispatch(system_taskq, zvol_prefetch_minors_impl, job,
1075 TQ_SLEEP);
1076
1077 zvol_add_clones(dsname, minors_list);
1078
1079 if (snapdev == ZFS_SNAPDEV_VISIBLE) {
1080 /*
1081 * traverse snapshots only, do not traverse children,
1082 * and skip the 'dsname'
1083 */
1084 (void) dmu_objset_find(dsname,
1085 zvol_create_snap_minor_cb, (void *)job,
1086 DS_FIND_SNAPSHOTS);
1087 }
1088 } else {
1089 dprintf("zvol_create_minors_cb(): %s is not a zvol name\n",
1090 dsname);
1091 }
1092
1093 return (0);
1094 }
1095
1096 /*
1097 * Create minors for the specified dataset, including children and snapshots.
1098 * Pay attention to the 'snapdev' property and iterate over the snapshots
1099 * only if they are 'visible'. This approach allows one to assure that the
1100 * snapshot metadata is read from disk only if it is needed.
1101 *
1102 * The name can represent a dataset to be recursively scanned for zvols and
1103 * their snapshots, or a single zvol snapshot. If the name represents a
1104 * dataset, the scan is performed in two nested stages:
1105 * - scan the dataset for zvols, and
1106 * - for each zvol, create a minor node, then check if the zvol's snapshots
1107 * are 'visible', and only then iterate over the snapshots if needed
1108 *
1109 * If the name represents a snapshot, a check is performed if the snapshot is
1110 * 'visible' (which also verifies that the parent is a zvol), and if so,
1111 * a minor node for that snapshot is created.
1112 */
1113 void
zvol_create_minors_recursive(const char * name)1114 zvol_create_minors_recursive(const char *name)
1115 {
1116 list_t minors_list;
1117 minors_job_t *job;
1118
1119 if (zvol_inhibit_dev)
1120 return;
1121
1122 /*
1123 * This is the list for prefetch jobs. Whenever we found a match
1124 * during dmu_objset_find, we insert a minors_job to the list and do
1125 * taskq_dispatch to parallel prefetch zvol dnodes. Note we don't need
1126 * any lock because all list operation is done on the current thread.
1127 *
1128 * We will use this list to do zvol_os_create_minor after prefetch
1129 * so we don't have to traverse using dmu_objset_find again.
1130 */
1131 list_create(&minors_list, sizeof (minors_job_t),
1132 offsetof(minors_job_t, link));
1133
1134
1135 if (strchr(name, '@') != NULL) {
1136 uint64_t snapdev;
1137
1138 int error = dsl_prop_get_integer(name, "snapdev",
1139 &snapdev, NULL);
1140
1141 if (error == 0 && snapdev == ZFS_SNAPDEV_VISIBLE)
1142 (void) zvol_os_create_minor(name);
1143 } else {
1144 fstrans_cookie_t cookie = spl_fstrans_mark();
1145 (void) dmu_objset_find(name, zvol_create_minors_cb,
1146 &minors_list, DS_FIND_CHILDREN);
1147 spl_fstrans_unmark(cookie);
1148 }
1149
1150 taskq_wait_outstanding(system_taskq, 0);
1151
1152 /*
1153 * Prefetch is completed, we can do zvol_os_create_minor
1154 * sequentially.
1155 */
1156 while ((job = list_remove_head(&minors_list)) != NULL) {
1157 if (!job->error)
1158 (void) zvol_os_create_minor(job->name);
1159 kmem_strfree(job->name);
1160 kmem_free(job, sizeof (minors_job_t));
1161 }
1162
1163 list_destroy(&minors_list);
1164 }
1165
1166 void
zvol_create_minor(const char * name)1167 zvol_create_minor(const char *name)
1168 {
1169 /*
1170 * Note: the dsl_pool_config_lock must not be held.
1171 * Minor node creation needs to obtain the zvol_state_lock.
1172 * zvol_open() obtains the zvol_state_lock and then the dsl pool
1173 * config lock. Therefore, we can't have the config lock now if
1174 * we are going to wait for the zvol_state_lock, because it
1175 * would be a lock order inversion which could lead to deadlock.
1176 */
1177
1178 if (zvol_inhibit_dev)
1179 return;
1180
1181 if (strchr(name, '@') != NULL) {
1182 uint64_t snapdev;
1183
1184 int error = dsl_prop_get_integer(name,
1185 "snapdev", &snapdev, NULL);
1186
1187 if (error == 0 && snapdev == ZFS_SNAPDEV_VISIBLE)
1188 (void) zvol_os_create_minor(name);
1189 } else {
1190 (void) zvol_os_create_minor(name);
1191 }
1192 }
1193
1194 /*
1195 * Remove minors for specified dataset including children and snapshots.
1196 */
1197
1198 static void
zvol_free_task(void * arg)1199 zvol_free_task(void *arg)
1200 {
1201 zvol_os_free(arg);
1202 }
1203
1204 void
zvol_remove_minors_impl(const char * name)1205 zvol_remove_minors_impl(const char *name)
1206 {
1207 zvol_state_t *zv, *zv_next;
1208 int namelen = ((name) ? strlen(name) : 0);
1209 taskqid_t t;
1210 list_t free_list;
1211
1212 if (zvol_inhibit_dev)
1213 return;
1214
1215 list_create(&free_list, sizeof (zvol_state_t),
1216 offsetof(zvol_state_t, zv_next));
1217
1218 rw_enter(&zvol_state_lock, RW_WRITER);
1219
1220 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1221 zv_next = list_next(&zvol_state_list, zv);
1222
1223 mutex_enter(&zv->zv_state_lock);
1224 if (name == NULL || strcmp(zv->zv_name, name) == 0 ||
1225 (strncmp(zv->zv_name, name, namelen) == 0 &&
1226 (zv->zv_name[namelen] == '/' ||
1227 zv->zv_name[namelen] == '@'))) {
1228 /*
1229 * By holding zv_state_lock here, we guarantee that no
1230 * one is currently using this zv
1231 */
1232
1233 /* If in use, leave alone */
1234 if (zv->zv_open_count > 0 ||
1235 atomic_read(&zv->zv_suspend_ref)) {
1236 mutex_exit(&zv->zv_state_lock);
1237 continue;
1238 }
1239
1240 zvol_remove(zv);
1241
1242 /*
1243 * Cleared while holding zvol_state_lock as a writer
1244 * which will prevent zvol_open() from opening it.
1245 */
1246 zvol_os_clear_private(zv);
1247
1248 /* Drop zv_state_lock before zvol_free() */
1249 mutex_exit(&zv->zv_state_lock);
1250
1251 /* Try parallel zv_free, if failed do it in place */
1252 t = taskq_dispatch(system_taskq, zvol_free_task, zv,
1253 TQ_SLEEP);
1254 if (t == TASKQID_INVALID)
1255 list_insert_head(&free_list, zv);
1256 } else {
1257 mutex_exit(&zv->zv_state_lock);
1258 }
1259 }
1260 rw_exit(&zvol_state_lock);
1261
1262 /* Drop zvol_state_lock before calling zvol_free() */
1263 while ((zv = list_remove_head(&free_list)) != NULL)
1264 zvol_os_free(zv);
1265 }
1266
1267 /* Remove minor for this specific volume only */
1268 static void
zvol_remove_minor_impl(const char * name)1269 zvol_remove_minor_impl(const char *name)
1270 {
1271 zvol_state_t *zv = NULL, *zv_next;
1272
1273 if (zvol_inhibit_dev)
1274 return;
1275
1276 rw_enter(&zvol_state_lock, RW_WRITER);
1277
1278 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1279 zv_next = list_next(&zvol_state_list, zv);
1280
1281 mutex_enter(&zv->zv_state_lock);
1282 if (strcmp(zv->zv_name, name) == 0) {
1283 /*
1284 * By holding zv_state_lock here, we guarantee that no
1285 * one is currently using this zv
1286 */
1287
1288 /* If in use, leave alone */
1289 if (zv->zv_open_count > 0 ||
1290 atomic_read(&zv->zv_suspend_ref)) {
1291 mutex_exit(&zv->zv_state_lock);
1292 continue;
1293 }
1294 zvol_remove(zv);
1295
1296 zvol_os_clear_private(zv);
1297 mutex_exit(&zv->zv_state_lock);
1298 break;
1299 } else {
1300 mutex_exit(&zv->zv_state_lock);
1301 }
1302 }
1303
1304 /* Drop zvol_state_lock before calling zvol_free() */
1305 rw_exit(&zvol_state_lock);
1306
1307 if (zv != NULL)
1308 zvol_os_free(zv);
1309 }
1310
1311 /*
1312 * Rename minors for specified dataset including children and snapshots.
1313 */
1314 static void
zvol_rename_minors_impl(const char * oldname,const char * newname)1315 zvol_rename_minors_impl(const char *oldname, const char *newname)
1316 {
1317 zvol_state_t *zv, *zv_next;
1318 int oldnamelen;
1319
1320 if (zvol_inhibit_dev)
1321 return;
1322
1323 oldnamelen = strlen(oldname);
1324
1325 rw_enter(&zvol_state_lock, RW_READER);
1326
1327 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1328 zv_next = list_next(&zvol_state_list, zv);
1329
1330 mutex_enter(&zv->zv_state_lock);
1331
1332 if (strcmp(zv->zv_name, oldname) == 0) {
1333 zvol_os_rename_minor(zv, newname);
1334 } else if (strncmp(zv->zv_name, oldname, oldnamelen) == 0 &&
1335 (zv->zv_name[oldnamelen] == '/' ||
1336 zv->zv_name[oldnamelen] == '@')) {
1337 char *name = kmem_asprintf("%s%c%s", newname,
1338 zv->zv_name[oldnamelen],
1339 zv->zv_name + oldnamelen + 1);
1340 zvol_os_rename_minor(zv, name);
1341 kmem_strfree(name);
1342 }
1343
1344 mutex_exit(&zv->zv_state_lock);
1345 }
1346
1347 rw_exit(&zvol_state_lock);
1348 }
1349
1350 typedef struct zvol_snapdev_cb_arg {
1351 uint64_t snapdev;
1352 } zvol_snapdev_cb_arg_t;
1353
1354 static int
zvol_set_snapdev_cb(const char * dsname,void * param)1355 zvol_set_snapdev_cb(const char *dsname, void *param)
1356 {
1357 zvol_snapdev_cb_arg_t *arg = param;
1358
1359 if (strchr(dsname, '@') == NULL)
1360 return (0);
1361
1362 switch (arg->snapdev) {
1363 case ZFS_SNAPDEV_VISIBLE:
1364 (void) zvol_os_create_minor(dsname);
1365 break;
1366 case ZFS_SNAPDEV_HIDDEN:
1367 (void) zvol_remove_minor_impl(dsname);
1368 break;
1369 }
1370
1371 return (0);
1372 }
1373
1374 static void
zvol_set_snapdev_impl(char * name,uint64_t snapdev)1375 zvol_set_snapdev_impl(char *name, uint64_t snapdev)
1376 {
1377 zvol_snapdev_cb_arg_t arg = {snapdev};
1378 fstrans_cookie_t cookie = spl_fstrans_mark();
1379 /*
1380 * The zvol_set_snapdev_sync() sets snapdev appropriately
1381 * in the dataset hierarchy. Here, we only scan snapshots.
1382 */
1383 dmu_objset_find(name, zvol_set_snapdev_cb, &arg, DS_FIND_SNAPSHOTS);
1384 spl_fstrans_unmark(cookie);
1385 }
1386
1387 static void
zvol_set_volmode_impl(char * name,uint64_t volmode)1388 zvol_set_volmode_impl(char *name, uint64_t volmode)
1389 {
1390 fstrans_cookie_t cookie;
1391 uint64_t old_volmode;
1392 zvol_state_t *zv;
1393
1394 if (strchr(name, '@') != NULL)
1395 return;
1396
1397 /*
1398 * It's unfortunate we need to remove minors before we create new ones:
1399 * this is necessary because our backing gendisk (zvol_state->zv_disk)
1400 * could be different when we set, for instance, volmode from "geom"
1401 * to "dev" (or vice versa).
1402 */
1403 zv = zvol_find_by_name(name, RW_NONE);
1404 if (zv == NULL && volmode == ZFS_VOLMODE_NONE)
1405 return;
1406 if (zv != NULL) {
1407 old_volmode = zv->zv_volmode;
1408 mutex_exit(&zv->zv_state_lock);
1409 if (old_volmode == volmode)
1410 return;
1411 zvol_wait_close(zv);
1412 }
1413 cookie = spl_fstrans_mark();
1414 switch (volmode) {
1415 case ZFS_VOLMODE_NONE:
1416 (void) zvol_remove_minor_impl(name);
1417 break;
1418 case ZFS_VOLMODE_GEOM:
1419 case ZFS_VOLMODE_DEV:
1420 (void) zvol_remove_minor_impl(name);
1421 (void) zvol_os_create_minor(name);
1422 break;
1423 case ZFS_VOLMODE_DEFAULT:
1424 (void) zvol_remove_minor_impl(name);
1425 if (zvol_volmode == ZFS_VOLMODE_NONE)
1426 break;
1427 else /* if zvol_volmode is invalid defaults to "geom" */
1428 (void) zvol_os_create_minor(name);
1429 break;
1430 }
1431 spl_fstrans_unmark(cookie);
1432 }
1433
1434 static zvol_task_t *
zvol_task_alloc(zvol_async_op_t op,const char * name1,const char * name2,uint64_t value)1435 zvol_task_alloc(zvol_async_op_t op, const char *name1, const char *name2,
1436 uint64_t value)
1437 {
1438 zvol_task_t *task;
1439
1440 /* Never allow tasks on hidden names. */
1441 if (name1[0] == '$')
1442 return (NULL);
1443
1444 task = kmem_zalloc(sizeof (zvol_task_t), KM_SLEEP);
1445 task->op = op;
1446 task->value = value;
1447
1448 strlcpy(task->name1, name1, MAXNAMELEN);
1449 if (name2 != NULL)
1450 strlcpy(task->name2, name2, MAXNAMELEN);
1451
1452 return (task);
1453 }
1454
1455 static void
zvol_task_free(zvol_task_t * task)1456 zvol_task_free(zvol_task_t *task)
1457 {
1458 kmem_free(task, sizeof (zvol_task_t));
1459 }
1460
1461 /*
1462 * The worker thread function performed asynchronously.
1463 */
1464 static void
zvol_task_cb(void * arg)1465 zvol_task_cb(void *arg)
1466 {
1467 zvol_task_t *task = arg;
1468
1469 switch (task->op) {
1470 case ZVOL_ASYNC_REMOVE_MINORS:
1471 zvol_remove_minors_impl(task->name1);
1472 break;
1473 case ZVOL_ASYNC_RENAME_MINORS:
1474 zvol_rename_minors_impl(task->name1, task->name2);
1475 break;
1476 case ZVOL_ASYNC_SET_SNAPDEV:
1477 zvol_set_snapdev_impl(task->name1, task->value);
1478 break;
1479 case ZVOL_ASYNC_SET_VOLMODE:
1480 zvol_set_volmode_impl(task->name1, task->value);
1481 break;
1482 default:
1483 VERIFY(0);
1484 break;
1485 }
1486
1487 zvol_task_free(task);
1488 }
1489
1490 typedef struct zvol_set_prop_int_arg {
1491 const char *zsda_name;
1492 uint64_t zsda_value;
1493 zprop_source_t zsda_source;
1494 dmu_tx_t *zsda_tx;
1495 } zvol_set_prop_int_arg_t;
1496
1497 /*
1498 * Sanity check the dataset for safe use by the sync task. No additional
1499 * conditions are imposed.
1500 */
1501 static int
zvol_set_snapdev_check(void * arg,dmu_tx_t * tx)1502 zvol_set_snapdev_check(void *arg, dmu_tx_t *tx)
1503 {
1504 zvol_set_prop_int_arg_t *zsda = arg;
1505 dsl_pool_t *dp = dmu_tx_pool(tx);
1506 dsl_dir_t *dd;
1507 int error;
1508
1509 error = dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL);
1510 if (error != 0)
1511 return (error);
1512
1513 dsl_dir_rele(dd, FTAG);
1514
1515 return (error);
1516 }
1517
1518 static int
zvol_set_snapdev_sync_cb(dsl_pool_t * dp,dsl_dataset_t * ds,void * arg)1519 zvol_set_snapdev_sync_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
1520 {
1521 (void) arg;
1522 char dsname[MAXNAMELEN];
1523 zvol_task_t *task;
1524 uint64_t snapdev;
1525
1526 dsl_dataset_name(ds, dsname);
1527 if (dsl_prop_get_int_ds(ds, "snapdev", &snapdev) != 0)
1528 return (0);
1529 task = zvol_task_alloc(ZVOL_ASYNC_SET_SNAPDEV, dsname, NULL, snapdev);
1530 if (task == NULL)
1531 return (0);
1532
1533 (void) taskq_dispatch(dp->dp_spa->spa_zvol_taskq, zvol_task_cb,
1534 task, TQ_SLEEP);
1535 return (0);
1536 }
1537
1538 /*
1539 * Traverse all child datasets and apply snapdev appropriately.
1540 * We call dsl_prop_set_sync_impl() here to set the value only on the toplevel
1541 * dataset and read the effective "snapdev" on every child in the callback
1542 * function: this is because the value is not guaranteed to be the same in the
1543 * whole dataset hierarchy.
1544 */
1545 static void
zvol_set_snapdev_sync(void * arg,dmu_tx_t * tx)1546 zvol_set_snapdev_sync(void *arg, dmu_tx_t *tx)
1547 {
1548 zvol_set_prop_int_arg_t *zsda = arg;
1549 dsl_pool_t *dp = dmu_tx_pool(tx);
1550 dsl_dir_t *dd;
1551 dsl_dataset_t *ds;
1552 int error;
1553
1554 VERIFY0(dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL));
1555 zsda->zsda_tx = tx;
1556
1557 error = dsl_dataset_hold(dp, zsda->zsda_name, FTAG, &ds);
1558 if (error == 0) {
1559 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_SNAPDEV),
1560 zsda->zsda_source, sizeof (zsda->zsda_value), 1,
1561 &zsda->zsda_value, zsda->zsda_tx);
1562 dsl_dataset_rele(ds, FTAG);
1563 }
1564 dmu_objset_find_dp(dp, dd->dd_object, zvol_set_snapdev_sync_cb,
1565 zsda, DS_FIND_CHILDREN);
1566
1567 dsl_dir_rele(dd, FTAG);
1568 }
1569
1570 int
zvol_set_snapdev(const char * ddname,zprop_source_t source,uint64_t snapdev)1571 zvol_set_snapdev(const char *ddname, zprop_source_t source, uint64_t snapdev)
1572 {
1573 zvol_set_prop_int_arg_t zsda;
1574
1575 zsda.zsda_name = ddname;
1576 zsda.zsda_source = source;
1577 zsda.zsda_value = snapdev;
1578
1579 return (dsl_sync_task(ddname, zvol_set_snapdev_check,
1580 zvol_set_snapdev_sync, &zsda, 0, ZFS_SPACE_CHECK_NONE));
1581 }
1582
1583 /*
1584 * Sanity check the dataset for safe use by the sync task. No additional
1585 * conditions are imposed.
1586 */
1587 static int
zvol_set_volmode_check(void * arg,dmu_tx_t * tx)1588 zvol_set_volmode_check(void *arg, dmu_tx_t *tx)
1589 {
1590 zvol_set_prop_int_arg_t *zsda = arg;
1591 dsl_pool_t *dp = dmu_tx_pool(tx);
1592 dsl_dir_t *dd;
1593 int error;
1594
1595 error = dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL);
1596 if (error != 0)
1597 return (error);
1598
1599 dsl_dir_rele(dd, FTAG);
1600
1601 return (error);
1602 }
1603
1604 static int
zvol_set_volmode_sync_cb(dsl_pool_t * dp,dsl_dataset_t * ds,void * arg)1605 zvol_set_volmode_sync_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
1606 {
1607 (void) arg;
1608 char dsname[MAXNAMELEN];
1609 zvol_task_t *task;
1610 uint64_t volmode;
1611
1612 dsl_dataset_name(ds, dsname);
1613 if (dsl_prop_get_int_ds(ds, "volmode", &volmode) != 0)
1614 return (0);
1615 task = zvol_task_alloc(ZVOL_ASYNC_SET_VOLMODE, dsname, NULL, volmode);
1616 if (task == NULL)
1617 return (0);
1618
1619 (void) taskq_dispatch(dp->dp_spa->spa_zvol_taskq, zvol_task_cb,
1620 task, TQ_SLEEP);
1621 return (0);
1622 }
1623
1624 /*
1625 * Traverse all child datasets and apply volmode appropriately.
1626 * We call dsl_prop_set_sync_impl() here to set the value only on the toplevel
1627 * dataset and read the effective "volmode" on every child in the callback
1628 * function: this is because the value is not guaranteed to be the same in the
1629 * whole dataset hierarchy.
1630 */
1631 static void
zvol_set_volmode_sync(void * arg,dmu_tx_t * tx)1632 zvol_set_volmode_sync(void *arg, dmu_tx_t *tx)
1633 {
1634 zvol_set_prop_int_arg_t *zsda = arg;
1635 dsl_pool_t *dp = dmu_tx_pool(tx);
1636 dsl_dir_t *dd;
1637 dsl_dataset_t *ds;
1638 int error;
1639
1640 VERIFY0(dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL));
1641 zsda->zsda_tx = tx;
1642
1643 error = dsl_dataset_hold(dp, zsda->zsda_name, FTAG, &ds);
1644 if (error == 0) {
1645 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_VOLMODE),
1646 zsda->zsda_source, sizeof (zsda->zsda_value), 1,
1647 &zsda->zsda_value, zsda->zsda_tx);
1648 dsl_dataset_rele(ds, FTAG);
1649 }
1650
1651 dmu_objset_find_dp(dp, dd->dd_object, zvol_set_volmode_sync_cb,
1652 zsda, DS_FIND_CHILDREN);
1653
1654 dsl_dir_rele(dd, FTAG);
1655 }
1656
1657 int
zvol_set_volmode(const char * ddname,zprop_source_t source,uint64_t volmode)1658 zvol_set_volmode(const char *ddname, zprop_source_t source, uint64_t volmode)
1659 {
1660 zvol_set_prop_int_arg_t zsda;
1661
1662 zsda.zsda_name = ddname;
1663 zsda.zsda_source = source;
1664 zsda.zsda_value = volmode;
1665
1666 return (dsl_sync_task(ddname, zvol_set_volmode_check,
1667 zvol_set_volmode_sync, &zsda, 0, ZFS_SPACE_CHECK_NONE));
1668 }
1669
1670 void
zvol_remove_minors(spa_t * spa,const char * name,boolean_t async)1671 zvol_remove_minors(spa_t *spa, const char *name, boolean_t async)
1672 {
1673 zvol_task_t *task;
1674 taskqid_t id;
1675
1676 task = zvol_task_alloc(ZVOL_ASYNC_REMOVE_MINORS, name, NULL, ~0ULL);
1677 if (task == NULL)
1678 return;
1679
1680 id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP);
1681 if ((async == B_FALSE) && (id != TASKQID_INVALID))
1682 taskq_wait_id(spa->spa_zvol_taskq, id);
1683 }
1684
1685 void
zvol_rename_minors(spa_t * spa,const char * name1,const char * name2,boolean_t async)1686 zvol_rename_minors(spa_t *spa, const char *name1, const char *name2,
1687 boolean_t async)
1688 {
1689 zvol_task_t *task;
1690 taskqid_t id;
1691
1692 task = zvol_task_alloc(ZVOL_ASYNC_RENAME_MINORS, name1, name2, ~0ULL);
1693 if (task == NULL)
1694 return;
1695
1696 id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP);
1697 if ((async == B_FALSE) && (id != TASKQID_INVALID))
1698 taskq_wait_id(spa->spa_zvol_taskq, id);
1699 }
1700
1701 boolean_t
zvol_is_zvol(const char * name)1702 zvol_is_zvol(const char *name)
1703 {
1704
1705 return (zvol_os_is_zvol(name));
1706 }
1707
1708 int
zvol_init_impl(void)1709 zvol_init_impl(void)
1710 {
1711 int i;
1712
1713 list_create(&zvol_state_list, sizeof (zvol_state_t),
1714 offsetof(zvol_state_t, zv_next));
1715 rw_init(&zvol_state_lock, NULL, RW_DEFAULT, NULL);
1716
1717 zvol_htable = kmem_alloc(ZVOL_HT_SIZE * sizeof (struct hlist_head),
1718 KM_SLEEP);
1719 for (i = 0; i < ZVOL_HT_SIZE; i++)
1720 INIT_HLIST_HEAD(&zvol_htable[i]);
1721
1722 return (0);
1723 }
1724
1725 void
zvol_fini_impl(void)1726 zvol_fini_impl(void)
1727 {
1728 zvol_remove_minors_impl(NULL);
1729
1730 /*
1731 * The call to "zvol_remove_minors_impl" may dispatch entries to
1732 * the system_taskq, but it doesn't wait for those entries to
1733 * complete before it returns. Thus, we must wait for all of the
1734 * removals to finish, before we can continue.
1735 */
1736 taskq_wait_outstanding(system_taskq, 0);
1737
1738 kmem_free(zvol_htable, ZVOL_HT_SIZE * sizeof (struct hlist_head));
1739 list_destroy(&zvol_state_list);
1740 rw_destroy(&zvol_state_lock);
1741 }
1742