]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zvol.c
Fix volume WR_INDIRECT log replay
[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 http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright (C) 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, 2017 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 * It is also worth keeping in mind that once add_disk() is called, the zvol is
72 * announced to the world, and zvol_open()/zvol_release() can be called at any
73 * time. Incidentally, add_disk() itself calls zvol_open()->zvol_first_open()
74 * and zvol_release()->zvol_last_close() directly as well.
75 */
76
77 #include <sys/dbuf.h>
78 #include <sys/dmu_traverse.h>
79 #include <sys/dsl_dataset.h>
80 #include <sys/dsl_prop.h>
81 #include <sys/dsl_dir.h>
82 #include <sys/zap.h>
83 #include <sys/zfeature.h>
84 #include <sys/zil_impl.h>
85 #include <sys/dmu_tx.h>
86 #include <sys/zio.h>
87 #include <sys/zfs_rlock.h>
88 #include <sys/zfs_znode.h>
89 #include <sys/spa_impl.h>
90 #include <sys/zvol.h>
91 #include <linux/blkdev_compat.h>
92
93 unsigned int zvol_inhibit_dev = 0;
94 unsigned int zvol_major = ZVOL_MAJOR;
95 unsigned int zvol_threads = 32;
96 unsigned int zvol_request_sync = 0;
97 unsigned int zvol_prefetch_bytes = (128 * 1024);
98 unsigned long zvol_max_discard_blocks = 16384;
99 unsigned int zvol_volmode = ZFS_VOLMODE_GEOM;
100
101 static taskq_t *zvol_taskq;
102 static kmutex_t zvol_state_lock;
103 static list_t zvol_state_list;
104
105 #define ZVOL_HT_SIZE 1024
106 static struct hlist_head *zvol_htable;
107 #define ZVOL_HT_HEAD(hash) (&zvol_htable[(hash) & (ZVOL_HT_SIZE-1)])
108
109 static struct ida zvol_ida;
110
111 /*
112 * The in-core state of each volume.
113 */
114 struct zvol_state {
115 char zv_name[MAXNAMELEN]; /* name */
116 uint64_t zv_volsize; /* advertised space */
117 uint64_t zv_volblocksize; /* volume block size */
118 objset_t *zv_objset; /* objset handle */
119 uint32_t zv_flags; /* ZVOL_* flags */
120 uint32_t zv_open_count; /* open counts */
121 uint32_t zv_changed; /* disk changed */
122 zilog_t *zv_zilog; /* ZIL handle */
123 zfs_rlock_t zv_range_lock; /* range lock */
124 dnode_t *zv_dn; /* dnode hold */
125 dev_t zv_dev; /* device id */
126 struct gendisk *zv_disk; /* generic disk */
127 struct request_queue *zv_queue; /* request queue */
128 list_node_t zv_next; /* next zvol_state_t linkage */
129 uint64_t zv_hash; /* name hash */
130 struct hlist_node zv_hlink; /* hash link */
131 kmutex_t zv_state_lock; /* protects zvol_state_t */
132 atomic_t zv_suspend_ref; /* refcount for suspend */
133 krwlock_t zv_suspend_lock; /* suspend lock */
134 };
135
136 typedef enum {
137 ZVOL_ASYNC_CREATE_MINORS,
138 ZVOL_ASYNC_REMOVE_MINORS,
139 ZVOL_ASYNC_RENAME_MINORS,
140 ZVOL_ASYNC_SET_SNAPDEV,
141 ZVOL_ASYNC_SET_VOLMODE,
142 ZVOL_ASYNC_MAX
143 } zvol_async_op_t;
144
145 typedef struct {
146 zvol_async_op_t op;
147 char pool[MAXNAMELEN];
148 char name1[MAXNAMELEN];
149 char name2[MAXNAMELEN];
150 zprop_source_t source;
151 uint64_t value;
152 } zvol_task_t;
153
154 #define ZVOL_RDONLY 0x1
155
156 static uint64_t
157 zvol_name_hash(const char *name)
158 {
159 int i;
160 uint64_t crc = -1ULL;
161 uint8_t *p = (uint8_t *)name;
162 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
163 for (i = 0; i < MAXNAMELEN - 1 && *p; i++, p++) {
164 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (*p)) & 0xFF];
165 }
166 return (crc);
167 }
168
169 /*
170 * Find a zvol_state_t given the full major+minor dev_t. If found,
171 * return with zv_state_lock taken, otherwise, return (NULL) without
172 * taking zv_state_lock.
173 */
174 static zvol_state_t *
175 zvol_find_by_dev(dev_t dev)
176 {
177 zvol_state_t *zv;
178
179 mutex_enter(&zvol_state_lock);
180 for (zv = list_head(&zvol_state_list); zv != NULL;
181 zv = list_next(&zvol_state_list, zv)) {
182 mutex_enter(&zv->zv_state_lock);
183 if (zv->zv_dev == dev) {
184 mutex_exit(&zvol_state_lock);
185 return (zv);
186 }
187 mutex_exit(&zv->zv_state_lock);
188 }
189 mutex_exit(&zvol_state_lock);
190
191 return (NULL);
192 }
193
194 /*
195 * Find a zvol_state_t given the name and hash generated by zvol_name_hash.
196 * If found, return with zv_suspend_lock and zv_state_lock taken, otherwise,
197 * return (NULL) without the taking locks. The zv_suspend_lock is always taken
198 * before zv_state_lock. The mode argument indicates the mode (including none)
199 * for zv_suspend_lock to be taken.
200 */
201 static zvol_state_t *
202 zvol_find_by_name_hash(const char *name, uint64_t hash, int mode)
203 {
204 zvol_state_t *zv;
205 struct hlist_node *p;
206
207 mutex_enter(&zvol_state_lock);
208 hlist_for_each(p, ZVOL_HT_HEAD(hash)) {
209 zv = hlist_entry(p, zvol_state_t, zv_hlink);
210 mutex_enter(&zv->zv_state_lock);
211 if (zv->zv_hash == hash &&
212 strncmp(zv->zv_name, name, MAXNAMELEN) == 0) {
213 /*
214 * this is the right zvol, take the locks in the
215 * right order
216 */
217 if (mode != RW_NONE &&
218 !rw_tryenter(&zv->zv_suspend_lock, mode)) {
219 mutex_exit(&zv->zv_state_lock);
220 rw_enter(&zv->zv_suspend_lock, mode);
221 mutex_enter(&zv->zv_state_lock);
222 /*
223 * zvol cannot be renamed as we continue
224 * to hold zvol_state_lock
225 */
226 ASSERT(zv->zv_hash == hash &&
227 strncmp(zv->zv_name, name, MAXNAMELEN)
228 == 0);
229 }
230 mutex_exit(&zvol_state_lock);
231 return (zv);
232 }
233 mutex_exit(&zv->zv_state_lock);
234 }
235 mutex_exit(&zvol_state_lock);
236
237 return (NULL);
238 }
239
240 /*
241 * Find a zvol_state_t given the name.
242 * If found, return with zv_suspend_lock and zv_state_lock taken, otherwise,
243 * return (NULL) without the taking locks. The zv_suspend_lock is always taken
244 * before zv_state_lock. The mode argument indicates the mode (including none)
245 * for zv_suspend_lock to be taken.
246 */
247 static zvol_state_t *
248 zvol_find_by_name(const char *name, int mode)
249 {
250 return (zvol_find_by_name_hash(name, zvol_name_hash(name), mode));
251 }
252
253
254 /*
255 * Given a path, return TRUE if path is a ZVOL.
256 */
257 boolean_t
258 zvol_is_zvol(const char *device)
259 {
260 struct block_device *bdev;
261 unsigned int major;
262
263 bdev = vdev_lookup_bdev(device);
264 if (IS_ERR(bdev))
265 return (B_FALSE);
266
267 major = MAJOR(bdev->bd_dev);
268 bdput(bdev);
269
270 if (major == zvol_major)
271 return (B_TRUE);
272
273 return (B_FALSE);
274 }
275
276 /*
277 * ZFS_IOC_CREATE callback handles dmu zvol and zap object creation.
278 */
279 void
280 zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
281 {
282 zfs_creat_t *zct = arg;
283 nvlist_t *nvprops = zct->zct_props;
284 int error;
285 uint64_t volblocksize, volsize;
286
287 VERIFY(nvlist_lookup_uint64(nvprops,
288 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
289 if (nvlist_lookup_uint64(nvprops,
290 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
291 volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
292
293 /*
294 * These properties must be removed from the list so the generic
295 * property setting step won't apply to them.
296 */
297 VERIFY(nvlist_remove_all(nvprops,
298 zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
299 (void) nvlist_remove_all(nvprops,
300 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
301
302 error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
303 DMU_OT_NONE, 0, tx);
304 ASSERT(error == 0);
305
306 error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
307 DMU_OT_NONE, 0, tx);
308 ASSERT(error == 0);
309
310 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
311 ASSERT(error == 0);
312 }
313
314 /*
315 * ZFS_IOC_OBJSET_STATS entry point.
316 */
317 int
318 zvol_get_stats(objset_t *os, nvlist_t *nv)
319 {
320 int error;
321 dmu_object_info_t *doi;
322 uint64_t val;
323
324 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
325 if (error)
326 return (SET_ERROR(error));
327
328 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
329 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
330 error = dmu_object_info(os, ZVOL_OBJ, doi);
331
332 if (error == 0) {
333 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
334 doi->doi_data_block_size);
335 }
336
337 kmem_free(doi, sizeof (dmu_object_info_t));
338
339 return (SET_ERROR(error));
340 }
341
342 static void
343 zvol_size_changed(zvol_state_t *zv, uint64_t volsize)
344 {
345 struct block_device *bdev;
346
347 ASSERT(MUTEX_HELD(&zv->zv_state_lock));
348
349 bdev = bdget_disk(zv->zv_disk, 0);
350 if (bdev == NULL)
351 return;
352
353 set_capacity(zv->zv_disk, volsize >> 9);
354 zv->zv_volsize = volsize;
355 check_disk_size_change(zv->zv_disk, bdev);
356
357 bdput(bdev);
358 }
359
360 /*
361 * Sanity check volume size.
362 */
363 int
364 zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
365 {
366 if (volsize == 0)
367 return (SET_ERROR(EINVAL));
368
369 if (volsize % blocksize != 0)
370 return (SET_ERROR(EINVAL));
371
372 #ifdef _ILP32
373 if (volsize - 1 > SPEC_MAXOFFSET_T)
374 return (SET_ERROR(EOVERFLOW));
375 #endif
376 return (0);
377 }
378
379 /*
380 * Ensure the zap is flushed then inform the VFS of the capacity change.
381 */
382 static int
383 zvol_update_volsize(uint64_t volsize, objset_t *os)
384 {
385 dmu_tx_t *tx;
386 int error;
387 uint64_t txg;
388
389 tx = dmu_tx_create(os);
390 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
391 dmu_tx_mark_netfree(tx);
392 error = dmu_tx_assign(tx, TXG_WAIT);
393 if (error) {
394 dmu_tx_abort(tx);
395 return (SET_ERROR(error));
396 }
397 txg = dmu_tx_get_txg(tx);
398
399 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
400 &volsize, tx);
401 dmu_tx_commit(tx);
402
403 txg_wait_synced(dmu_objset_pool(os), txg);
404
405 if (error == 0)
406 error = dmu_free_long_range(os,
407 ZVOL_OBJ, volsize, DMU_OBJECT_END);
408
409 return (error);
410 }
411
412 static int
413 zvol_update_live_volsize(zvol_state_t *zv, uint64_t volsize)
414 {
415 zvol_size_changed(zv, volsize);
416
417 /*
418 * We should post a event here describing the expansion. However,
419 * the zfs_ereport_post() interface doesn't nicely support posting
420 * events for zvols, it assumes events relate to vdevs or zios.
421 */
422
423 return (0);
424 }
425
426 /*
427 * Set ZFS_PROP_VOLSIZE set entry point.
428 */
429 int
430 zvol_set_volsize(const char *name, uint64_t volsize)
431 {
432 zvol_state_t *zv = NULL;
433 objset_t *os = NULL;
434 int error;
435 dmu_object_info_t *doi;
436 uint64_t readonly;
437 boolean_t owned = B_FALSE;
438
439 error = dsl_prop_get_integer(name,
440 zfs_prop_to_name(ZFS_PROP_READONLY), &readonly, NULL);
441 if (error != 0)
442 return (SET_ERROR(error));
443 if (readonly)
444 return (SET_ERROR(EROFS));
445
446 zv = zvol_find_by_name(name, RW_READER);
447
448 ASSERT(zv == NULL || (MUTEX_HELD(&zv->zv_state_lock) &&
449 RW_READ_HELD(&zv->zv_suspend_lock)));
450
451 if (zv == NULL || zv->zv_objset == NULL) {
452 if (zv != NULL)
453 rw_exit(&zv->zv_suspend_lock);
454 if ((error = dmu_objset_own(name, DMU_OST_ZVOL, B_FALSE, B_TRUE,
455 FTAG, &os)) != 0) {
456 if (zv != NULL)
457 mutex_exit(&zv->zv_state_lock);
458 return (SET_ERROR(error));
459 }
460 owned = B_TRUE;
461 if (zv != NULL)
462 zv->zv_objset = os;
463 } else {
464 os = zv->zv_objset;
465 }
466
467 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
468
469 if ((error = dmu_object_info(os, ZVOL_OBJ, doi)) ||
470 (error = zvol_check_volsize(volsize, doi->doi_data_block_size)))
471 goto out;
472
473 error = zvol_update_volsize(volsize, os);
474
475 if (error == 0 && zv != NULL)
476 error = zvol_update_live_volsize(zv, volsize);
477 out:
478 kmem_free(doi, sizeof (dmu_object_info_t));
479
480 if (owned) {
481 dmu_objset_disown(os, B_TRUE, FTAG);
482 if (zv != NULL)
483 zv->zv_objset = NULL;
484 } else {
485 rw_exit(&zv->zv_suspend_lock);
486 }
487
488 if (zv != NULL)
489 mutex_exit(&zv->zv_state_lock);
490
491 return (SET_ERROR(error));
492 }
493
494 /*
495 * Sanity check volume block size.
496 */
497 int
498 zvol_check_volblocksize(const char *name, uint64_t volblocksize)
499 {
500 /* Record sizes above 128k need the feature to be enabled */
501 if (volblocksize > SPA_OLD_MAXBLOCKSIZE) {
502 spa_t *spa;
503 int error;
504
505 if ((error = spa_open(name, &spa, FTAG)) != 0)
506 return (error);
507
508 if (!spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) {
509 spa_close(spa, FTAG);
510 return (SET_ERROR(ENOTSUP));
511 }
512
513 /*
514 * We don't allow setting the property above 1MB,
515 * unless the tunable has been changed.
516 */
517 if (volblocksize > zfs_max_recordsize)
518 return (SET_ERROR(EDOM));
519
520 spa_close(spa, FTAG);
521 }
522
523 if (volblocksize < SPA_MINBLOCKSIZE ||
524 volblocksize > SPA_MAXBLOCKSIZE ||
525 !ISP2(volblocksize))
526 return (SET_ERROR(EDOM));
527
528 return (0);
529 }
530
531 /*
532 * Set ZFS_PROP_VOLBLOCKSIZE set entry point.
533 */
534 int
535 zvol_set_volblocksize(const char *name, uint64_t volblocksize)
536 {
537 zvol_state_t *zv;
538 dmu_tx_t *tx;
539 int error;
540
541 zv = zvol_find_by_name(name, RW_READER);
542
543 if (zv == NULL)
544 return (SET_ERROR(ENXIO));
545
546 ASSERT(MUTEX_HELD(&zv->zv_state_lock) &&
547 RW_READ_HELD(&zv->zv_suspend_lock));
548
549 if (zv->zv_flags & ZVOL_RDONLY) {
550 mutex_exit(&zv->zv_state_lock);
551 rw_exit(&zv->zv_suspend_lock);
552 return (SET_ERROR(EROFS));
553 }
554
555 tx = dmu_tx_create(zv->zv_objset);
556 dmu_tx_hold_bonus(tx, ZVOL_OBJ);
557 error = dmu_tx_assign(tx, TXG_WAIT);
558 if (error) {
559 dmu_tx_abort(tx);
560 } else {
561 error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ,
562 volblocksize, 0, tx);
563 if (error == ENOTSUP)
564 error = SET_ERROR(EBUSY);
565 dmu_tx_commit(tx);
566 if (error == 0)
567 zv->zv_volblocksize = volblocksize;
568 }
569
570 mutex_exit(&zv->zv_state_lock);
571 rw_exit(&zv->zv_suspend_lock);
572
573 return (SET_ERROR(error));
574 }
575
576 /*
577 * Replay a TX_TRUNCATE ZIL transaction if asked. TX_TRUNCATE is how we
578 * implement DKIOCFREE/free-long-range.
579 */
580 static int
581 zvol_replay_truncate(zvol_state_t *zv, lr_truncate_t *lr, boolean_t byteswap)
582 {
583 uint64_t offset, length;
584
585 if (byteswap)
586 byteswap_uint64_array(lr, sizeof (*lr));
587
588 offset = lr->lr_offset;
589 length = lr->lr_length;
590
591 return (dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, offset, length));
592 }
593
594 /*
595 * Replay a TX_WRITE ZIL transaction that didn't get committed
596 * after a system failure
597 */
598 static int
599 zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
600 {
601 objset_t *os = zv->zv_objset;
602 char *data = (char *)(lr + 1); /* data follows lr_write_t */
603 uint64_t offset, length;
604 dmu_tx_t *tx;
605 int error;
606
607 if (byteswap)
608 byteswap_uint64_array(lr, sizeof (*lr));
609
610 offset = lr->lr_offset;
611 length = lr->lr_length;
612
613 /* If it's a dmu_sync() block, write the whole block */
614 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
615 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
616 if (length < blocksize) {
617 offset -= offset % blocksize;
618 length = blocksize;
619 }
620 }
621
622 tx = dmu_tx_create(os);
623 dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length);
624 error = dmu_tx_assign(tx, TXG_WAIT);
625 if (error) {
626 dmu_tx_abort(tx);
627 } else {
628 dmu_write(os, ZVOL_OBJ, offset, length, data, tx);
629 dmu_tx_commit(tx);
630 }
631
632 return (error);
633 }
634
635 static int
636 zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
637 {
638 return (SET_ERROR(ENOTSUP));
639 }
640
641 /*
642 * Callback vectors for replaying records.
643 * Only TX_WRITE and TX_TRUNCATE are needed for zvol.
644 */
645 zil_replay_func_t zvol_replay_vector[TX_MAX_TYPE] = {
646 (zil_replay_func_t)zvol_replay_err, /* no such transaction type */
647 (zil_replay_func_t)zvol_replay_err, /* TX_CREATE */
648 (zil_replay_func_t)zvol_replay_err, /* TX_MKDIR */
649 (zil_replay_func_t)zvol_replay_err, /* TX_MKXATTR */
650 (zil_replay_func_t)zvol_replay_err, /* TX_SYMLINK */
651 (zil_replay_func_t)zvol_replay_err, /* TX_REMOVE */
652 (zil_replay_func_t)zvol_replay_err, /* TX_RMDIR */
653 (zil_replay_func_t)zvol_replay_err, /* TX_LINK */
654 (zil_replay_func_t)zvol_replay_err, /* TX_RENAME */
655 (zil_replay_func_t)zvol_replay_write, /* TX_WRITE */
656 (zil_replay_func_t)zvol_replay_truncate, /* TX_TRUNCATE */
657 (zil_replay_func_t)zvol_replay_err, /* TX_SETATTR */
658 (zil_replay_func_t)zvol_replay_err, /* TX_ACL */
659 };
660
661 /*
662 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
663 *
664 * We store data in the log buffers if it's small enough.
665 * Otherwise we will later flush the data out via dmu_sync().
666 */
667 ssize_t zvol_immediate_write_sz = 32768;
668
669 static void
670 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, uint64_t offset,
671 uint64_t size, int sync)
672 {
673 uint32_t blocksize = zv->zv_volblocksize;
674 zilog_t *zilog = zv->zv_zilog;
675 itx_wr_state_t write_state;
676
677 if (zil_replaying(zilog, tx))
678 return;
679
680 if (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
681 write_state = WR_INDIRECT;
682 else if (!spa_has_slogs(zilog->zl_spa) &&
683 size >= blocksize && blocksize > zvol_immediate_write_sz)
684 write_state = WR_INDIRECT;
685 else if (sync)
686 write_state = WR_COPIED;
687 else
688 write_state = WR_NEED_COPY;
689
690 while (size) {
691 itx_t *itx;
692 lr_write_t *lr;
693 itx_wr_state_t wr_state = write_state;
694 ssize_t len = size;
695
696 if (wr_state == WR_COPIED && size > ZIL_MAX_COPIED_DATA)
697 wr_state = WR_NEED_COPY;
698 else if (wr_state == WR_INDIRECT)
699 len = MIN(blocksize - P2PHASE(offset, blocksize), size);
700
701 itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
702 (wr_state == WR_COPIED ? len : 0));
703 lr = (lr_write_t *)&itx->itx_lr;
704 if (wr_state == WR_COPIED && dmu_read_by_dnode(zv->zv_dn,
705 offset, len, lr+1, DMU_READ_NO_PREFETCH) != 0) {
706 zil_itx_destroy(itx);
707 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
708 lr = (lr_write_t *)&itx->itx_lr;
709 wr_state = WR_NEED_COPY;
710 }
711
712 itx->itx_wr_state = wr_state;
713 lr->lr_foid = ZVOL_OBJ;
714 lr->lr_offset = offset;
715 lr->lr_length = len;
716 lr->lr_blkoff = 0;
717 BP_ZERO(&lr->lr_blkptr);
718
719 itx->itx_private = zv;
720 itx->itx_sync = sync;
721
722 (void) zil_itx_assign(zilog, itx, tx);
723
724 offset += len;
725 size -= len;
726 }
727 }
728
729 typedef struct zv_request {
730 zvol_state_t *zv;
731 struct bio *bio;
732 rl_t *rl;
733 } zv_request_t;
734
735 static void
736 uio_from_bio(uio_t *uio, struct bio *bio)
737 {
738 uio->uio_bvec = &bio->bi_io_vec[BIO_BI_IDX(bio)];
739 uio->uio_skip = BIO_BI_SKIP(bio);
740 uio->uio_resid = BIO_BI_SIZE(bio);
741 uio->uio_iovcnt = bio->bi_vcnt - BIO_BI_IDX(bio);
742 uio->uio_loffset = BIO_BI_SECTOR(bio) << 9;
743 uio->uio_limit = MAXOFFSET_T;
744 uio->uio_segflg = UIO_BVEC;
745 }
746
747 static void
748 zvol_write(void *arg)
749 {
750 zv_request_t *zvr = arg;
751 struct bio *bio = zvr->bio;
752 uio_t uio;
753 zvol_state_t *zv = zvr->zv;
754 uint64_t volsize = zv->zv_volsize;
755 boolean_t sync;
756 int error = 0;
757 unsigned long start_jif;
758
759 uio_from_bio(&uio, bio);
760
761 ASSERT(zv && zv->zv_open_count > 0);
762
763 start_jif = jiffies;
764 generic_start_io_acct(WRITE, bio_sectors(bio), &zv->zv_disk->part0);
765
766 sync = bio_is_fua(bio) || zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS;
767
768 while (uio.uio_resid > 0 && uio.uio_loffset < volsize) {
769 uint64_t bytes = MIN(uio.uio_resid, DMU_MAX_ACCESS >> 1);
770 uint64_t off = uio.uio_loffset;
771 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
772
773 if (bytes > volsize - off) /* don't write past the end */
774 bytes = volsize - off;
775
776 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
777
778 /* This will only fail for ENOSPC */
779 error = dmu_tx_assign(tx, TXG_WAIT);
780 if (error) {
781 dmu_tx_abort(tx);
782 break;
783 }
784 error = dmu_write_uio_dnode(zv->zv_dn, &uio, bytes, tx);
785 if (error == 0)
786 zvol_log_write(zv, tx, off, bytes, sync);
787 dmu_tx_commit(tx);
788
789 if (error)
790 break;
791 }
792 zfs_range_unlock(zvr->rl);
793 if (sync)
794 zil_commit(zv->zv_zilog, ZVOL_OBJ);
795
796 rw_exit(&zv->zv_suspend_lock);
797 generic_end_io_acct(WRITE, &zv->zv_disk->part0, start_jif);
798 BIO_END_IO(bio, -error);
799 kmem_free(zvr, sizeof (zv_request_t));
800 }
801
802 /*
803 * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE.
804 */
805 static void
806 zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off, uint64_t len,
807 boolean_t sync)
808 {
809 itx_t *itx;
810 lr_truncate_t *lr;
811 zilog_t *zilog = zv->zv_zilog;
812
813 if (zil_replaying(zilog, tx))
814 return;
815
816 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
817 lr = (lr_truncate_t *)&itx->itx_lr;
818 lr->lr_foid = ZVOL_OBJ;
819 lr->lr_offset = off;
820 lr->lr_length = len;
821
822 itx->itx_sync = sync;
823 zil_itx_assign(zilog, itx, tx);
824 }
825
826 static void
827 zvol_discard(void *arg)
828 {
829 zv_request_t *zvr = arg;
830 struct bio *bio = zvr->bio;
831 zvol_state_t *zv = zvr->zv;
832 uint64_t start = BIO_BI_SECTOR(bio) << 9;
833 uint64_t size = BIO_BI_SIZE(bio);
834 uint64_t end = start + size;
835 boolean_t sync;
836 int error = 0;
837 dmu_tx_t *tx;
838 unsigned long start_jif;
839
840 ASSERT(zv && zv->zv_open_count > 0);
841
842 start_jif = jiffies;
843 generic_start_io_acct(WRITE, bio_sectors(bio), &zv->zv_disk->part0);
844
845 sync = bio_is_fua(bio) || zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS;
846
847 if (end > zv->zv_volsize) {
848 error = SET_ERROR(EIO);
849 goto unlock;
850 }
851
852 /*
853 * Align the request to volume block boundaries when a secure erase is
854 * not required. This will prevent dnode_free_range() from zeroing out
855 * the unaligned parts which is slow (read-modify-write) and useless
856 * since we are not freeing any space by doing so.
857 */
858 if (!bio_is_secure_erase(bio)) {
859 start = P2ROUNDUP(start, zv->zv_volblocksize);
860 end = P2ALIGN(end, zv->zv_volblocksize);
861 size = end - start;
862 }
863
864 if (start >= end)
865 goto unlock;
866
867 tx = dmu_tx_create(zv->zv_objset);
868 dmu_tx_mark_netfree(tx);
869 error = dmu_tx_assign(tx, TXG_WAIT);
870 if (error != 0) {
871 dmu_tx_abort(tx);
872 } else {
873 zvol_log_truncate(zv, tx, start, size, B_TRUE);
874 dmu_tx_commit(tx);
875 error = dmu_free_long_range(zv->zv_objset,
876 ZVOL_OBJ, start, size);
877 }
878 unlock:
879 zfs_range_unlock(zvr->rl);
880 if (error == 0 && sync)
881 zil_commit(zv->zv_zilog, ZVOL_OBJ);
882
883 rw_exit(&zv->zv_suspend_lock);
884 generic_end_io_acct(WRITE, &zv->zv_disk->part0, start_jif);
885 BIO_END_IO(bio, -error);
886 kmem_free(zvr, sizeof (zv_request_t));
887 }
888
889 static void
890 zvol_read(void *arg)
891 {
892 zv_request_t *zvr = arg;
893 struct bio *bio = zvr->bio;
894 uio_t uio;
895 zvol_state_t *zv = zvr->zv;
896 uint64_t volsize = zv->zv_volsize;
897 int error = 0;
898 unsigned long start_jif;
899
900 uio_from_bio(&uio, bio);
901
902 ASSERT(zv && zv->zv_open_count > 0);
903
904 start_jif = jiffies;
905 generic_start_io_acct(READ, bio_sectors(bio), &zv->zv_disk->part0);
906
907 while (uio.uio_resid > 0 && uio.uio_loffset < volsize) {
908 uint64_t bytes = MIN(uio.uio_resid, DMU_MAX_ACCESS >> 1);
909
910 /* don't read past the end */
911 if (bytes > volsize - uio.uio_loffset)
912 bytes = volsize - uio.uio_loffset;
913
914 error = dmu_read_uio_dnode(zv->zv_dn, &uio, bytes);
915 if (error) {
916 /* convert checksum errors into IO errors */
917 if (error == ECKSUM)
918 error = SET_ERROR(EIO);
919 break;
920 }
921 }
922 zfs_range_unlock(zvr->rl);
923
924 rw_exit(&zv->zv_suspend_lock);
925 generic_end_io_acct(READ, &zv->zv_disk->part0, start_jif);
926 BIO_END_IO(bio, -error);
927 kmem_free(zvr, sizeof (zv_request_t));
928 }
929
930 static MAKE_REQUEST_FN_RET
931 zvol_request(struct request_queue *q, struct bio *bio)
932 {
933 zvol_state_t *zv = q->queuedata;
934 fstrans_cookie_t cookie = spl_fstrans_mark();
935 uint64_t offset = BIO_BI_SECTOR(bio) << 9;
936 uint64_t size = BIO_BI_SIZE(bio);
937 int rw = bio_data_dir(bio);
938 zv_request_t *zvr;
939
940 if (bio_has_data(bio) && offset + size > zv->zv_volsize) {
941 printk(KERN_INFO
942 "%s: bad access: offset=%llu, size=%lu\n",
943 zv->zv_disk->disk_name,
944 (long long unsigned)offset,
945 (long unsigned)size);
946
947 BIO_END_IO(bio, -SET_ERROR(EIO));
948 goto out;
949 }
950
951 if (rw == WRITE) {
952 boolean_t need_sync = B_FALSE;
953
954 if (unlikely(zv->zv_flags & ZVOL_RDONLY)) {
955 BIO_END_IO(bio, -SET_ERROR(EROFS));
956 goto out;
957 }
958
959 /*
960 * To be released in the I/O function. See the comment on
961 * zfs_range_lock below.
962 */
963 rw_enter(&zv->zv_suspend_lock, RW_READER);
964
965 /* bio marked as FLUSH need to flush before write */
966 if (bio_is_flush(bio))
967 zil_commit(zv->zv_zilog, ZVOL_OBJ);
968
969 /* Some requests are just for flush and nothing else. */
970 if (size == 0) {
971 rw_exit(&zv->zv_suspend_lock);
972 BIO_END_IO(bio, 0);
973 goto out;
974 }
975
976 zvr = kmem_alloc(sizeof (zv_request_t), KM_SLEEP);
977 zvr->zv = zv;
978 zvr->bio = bio;
979
980 /*
981 * To be released in the I/O function. Since the I/O functions
982 * are asynchronous, we take it here synchronously to make
983 * sure overlapped I/Os are properly ordered.
984 */
985 zvr->rl = zfs_range_lock(&zv->zv_range_lock, offset, size,
986 RL_WRITER);
987 /*
988 * Sync writes and discards execute zil_commit() which may need
989 * to take a RL_READER lock on the whole block being modified
990 * via its zillog->zl_get_data(): to avoid circular dependency
991 * issues with taskq threads execute these requests
992 * synchronously here in zvol_request().
993 */
994 need_sync = bio_is_fua(bio) ||
995 zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS;
996 if (bio_is_discard(bio) || bio_is_secure_erase(bio)) {
997 if (zvol_request_sync || need_sync ||
998 taskq_dispatch(zvol_taskq, zvol_discard, zvr,
999 TQ_SLEEP) == TASKQID_INVALID)
1000 zvol_discard(zvr);
1001 } else {
1002 if (zvol_request_sync || need_sync ||
1003 taskq_dispatch(zvol_taskq, zvol_write, zvr,
1004 TQ_SLEEP) == TASKQID_INVALID)
1005 zvol_write(zvr);
1006 }
1007 } else {
1008 zvr = kmem_alloc(sizeof (zv_request_t), KM_SLEEP);
1009 zvr->zv = zv;
1010 zvr->bio = bio;
1011
1012 rw_enter(&zv->zv_suspend_lock, RW_READER);
1013
1014 zvr->rl = zfs_range_lock(&zv->zv_range_lock, offset, size,
1015 RL_READER);
1016 if (zvol_request_sync || taskq_dispatch(zvol_taskq,
1017 zvol_read, zvr, TQ_SLEEP) == TASKQID_INVALID)
1018 zvol_read(zvr);
1019 }
1020
1021 out:
1022 spl_fstrans_unmark(cookie);
1023 #ifdef HAVE_MAKE_REQUEST_FN_RET_INT
1024 return (0);
1025 #elif defined(HAVE_MAKE_REQUEST_FN_RET_QC)
1026 return (BLK_QC_T_NONE);
1027 #endif
1028 }
1029
1030 static void
1031 zvol_get_done(zgd_t *zgd, int error)
1032 {
1033 if (zgd->zgd_db)
1034 dmu_buf_rele(zgd->zgd_db, zgd);
1035
1036 zfs_range_unlock(zgd->zgd_rl);
1037
1038 if (error == 0 && zgd->zgd_bp)
1039 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1040
1041 kmem_free(zgd, sizeof (zgd_t));
1042 }
1043
1044 /*
1045 * Get data to generate a TX_WRITE intent log record.
1046 */
1047 static int
1048 zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1049 {
1050 zvol_state_t *zv = arg;
1051 uint64_t offset = lr->lr_offset;
1052 uint64_t size = lr->lr_length;
1053 dmu_buf_t *db;
1054 zgd_t *zgd;
1055 int error;
1056
1057 ASSERT(zio != NULL);
1058 ASSERT(size != 0);
1059
1060 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1061 zgd->zgd_zilog = zv->zv_zilog;
1062
1063 /*
1064 * Write records come in two flavors: immediate and indirect.
1065 * For small writes it's cheaper to store the data with the
1066 * log record (immediate); for large writes it's cheaper to
1067 * sync the data and get a pointer to it (indirect) so that
1068 * we don't have to write the data twice.
1069 */
1070 if (buf != NULL) { /* immediate write */
1071 zgd->zgd_rl = zfs_range_lock(&zv->zv_range_lock, offset, size,
1072 RL_READER);
1073 error = dmu_read_by_dnode(zv->zv_dn, offset, size, buf,
1074 DMU_READ_NO_PREFETCH);
1075 } else { /* indirect write */
1076 /*
1077 * Have to lock the whole block to ensure when it's written out
1078 * and its checksum is being calculated that no one can change
1079 * the data. Contrarily to zfs_get_data we need not re-check
1080 * blocksize after we get the lock because it cannot be changed.
1081 */
1082 size = zv->zv_volblocksize;
1083 offset = P2ALIGN_TYPED(offset, size, uint64_t);
1084 zgd->zgd_rl = zfs_range_lock(&zv->zv_range_lock, offset, size,
1085 RL_READER);
1086 error = dmu_buf_hold_by_dnode(zv->zv_dn, offset, zgd, &db,
1087 DMU_READ_NO_PREFETCH);
1088 if (error == 0) {
1089 blkptr_t *bp = &lr->lr_blkptr;
1090
1091 zgd->zgd_db = db;
1092 zgd->zgd_bp = bp;
1093
1094 ASSERT(db != NULL);
1095 ASSERT(db->db_offset == offset);
1096 ASSERT(db->db_size == size);
1097
1098 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1099 zvol_get_done, zgd);
1100
1101 if (error == 0)
1102 return (0);
1103 }
1104 }
1105
1106 zvol_get_done(zgd, error);
1107
1108 return (SET_ERROR(error));
1109 }
1110
1111 /*
1112 * The zvol_state_t's are inserted into zvol_state_list and zvol_htable.
1113 */
1114 static void
1115 zvol_insert(zvol_state_t *zv)
1116 {
1117 ASSERT(MUTEX_HELD(&zvol_state_lock));
1118 ASSERT3U(MINOR(zv->zv_dev) & ZVOL_MINOR_MASK, ==, 0);
1119 list_insert_head(&zvol_state_list, zv);
1120 hlist_add_head(&zv->zv_hlink, ZVOL_HT_HEAD(zv->zv_hash));
1121 }
1122
1123 /*
1124 * Simply remove the zvol from to list of zvols.
1125 */
1126 static void
1127 zvol_remove(zvol_state_t *zv)
1128 {
1129 ASSERT(MUTEX_HELD(&zvol_state_lock));
1130 list_remove(&zvol_state_list, zv);
1131 hlist_del(&zv->zv_hlink);
1132 }
1133
1134 /*
1135 * Setup zv after we just own the zv->objset
1136 */
1137 static int
1138 zvol_setup_zv(zvol_state_t *zv)
1139 {
1140 uint64_t volsize;
1141 int error;
1142 uint64_t ro;
1143 objset_t *os = zv->zv_objset;
1144
1145 ASSERT(MUTEX_HELD(&zv->zv_state_lock) &&
1146 RW_LOCK_HELD(&zv->zv_suspend_lock));
1147
1148 error = dsl_prop_get_integer(zv->zv_name, "readonly", &ro, NULL);
1149 if (error)
1150 return (SET_ERROR(error));
1151
1152 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
1153 if (error)
1154 return (SET_ERROR(error));
1155
1156 error = dnode_hold(os, ZVOL_OBJ, FTAG, &zv->zv_dn);
1157 if (error)
1158 return (SET_ERROR(error));
1159
1160 set_capacity(zv->zv_disk, volsize >> 9);
1161 zv->zv_volsize = volsize;
1162 zv->zv_zilog = zil_open(os, zvol_get_data);
1163
1164 if (ro || dmu_objset_is_snapshot(os) ||
1165 !spa_writeable(dmu_objset_spa(os))) {
1166 set_disk_ro(zv->zv_disk, 1);
1167 zv->zv_flags |= ZVOL_RDONLY;
1168 } else {
1169 set_disk_ro(zv->zv_disk, 0);
1170 zv->zv_flags &= ~ZVOL_RDONLY;
1171 }
1172 return (0);
1173 }
1174
1175 /*
1176 * Shutdown every zv_objset related stuff except zv_objset itself.
1177 * The is the reverse of zvol_setup_zv.
1178 */
1179 static void
1180 zvol_shutdown_zv(zvol_state_t *zv)
1181 {
1182 ASSERT(MUTEX_HELD(&zv->zv_state_lock) &&
1183 RW_LOCK_HELD(&zv->zv_suspend_lock));
1184
1185 zil_close(zv->zv_zilog);
1186 zv->zv_zilog = NULL;
1187
1188 dnode_rele(zv->zv_dn, FTAG);
1189 zv->zv_dn = NULL;
1190
1191 /*
1192 * Evict cached data
1193 */
1194 if (dsl_dataset_is_dirty(dmu_objset_ds(zv->zv_objset)) &&
1195 !(zv->zv_flags & ZVOL_RDONLY))
1196 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
1197 (void) dmu_objset_evict_dbufs(zv->zv_objset);
1198 }
1199
1200 /*
1201 * return the proper tag for rollback and recv
1202 */
1203 void *
1204 zvol_tag(zvol_state_t *zv)
1205 {
1206 ASSERT(RW_WRITE_HELD(&zv->zv_suspend_lock));
1207 return (zv->zv_open_count > 0 ? zv : NULL);
1208 }
1209
1210 /*
1211 * Suspend the zvol for recv and rollback.
1212 */
1213 zvol_state_t *
1214 zvol_suspend(const char *name)
1215 {
1216 zvol_state_t *zv;
1217
1218 zv = zvol_find_by_name(name, RW_WRITER);
1219
1220 if (zv == NULL)
1221 return (NULL);
1222
1223 /* block all I/O, release in zvol_resume. */
1224 ASSERT(MUTEX_HELD(&zv->zv_state_lock) &&
1225 RW_WRITE_HELD(&zv->zv_suspend_lock));
1226
1227 atomic_inc(&zv->zv_suspend_ref);
1228
1229 if (zv->zv_open_count > 0)
1230 zvol_shutdown_zv(zv);
1231
1232 /*
1233 * do not hold zv_state_lock across suspend/resume to
1234 * avoid locking up zvol lookups
1235 */
1236 mutex_exit(&zv->zv_state_lock);
1237
1238 /* zv_suspend_lock is released in zvol_resume() */
1239 return (zv);
1240 }
1241
1242 int
1243 zvol_resume(zvol_state_t *zv)
1244 {
1245 int error = 0;
1246
1247 ASSERT(RW_WRITE_HELD(&zv->zv_suspend_lock));
1248
1249 mutex_enter(&zv->zv_state_lock);
1250
1251 if (zv->zv_open_count > 0) {
1252 VERIFY0(dmu_objset_hold(zv->zv_name, zv, &zv->zv_objset));
1253 VERIFY3P(zv->zv_objset->os_dsl_dataset->ds_owner, ==, zv);
1254 VERIFY(dsl_dataset_long_held(zv->zv_objset->os_dsl_dataset));
1255 dmu_objset_rele(zv->zv_objset, zv);
1256
1257 error = zvol_setup_zv(zv);
1258 }
1259
1260 mutex_exit(&zv->zv_state_lock);
1261
1262 rw_exit(&zv->zv_suspend_lock);
1263 /*
1264 * We need this because we don't hold zvol_state_lock while releasing
1265 * zv_suspend_lock. zvol_remove_minors_impl thus cannot check
1266 * zv_suspend_lock to determine it is safe to free because rwlock is
1267 * not inherent atomic.
1268 */
1269 atomic_dec(&zv->zv_suspend_ref);
1270
1271 return (SET_ERROR(error));
1272 }
1273
1274 static int
1275 zvol_first_open(zvol_state_t *zv)
1276 {
1277 objset_t *os;
1278 int error, locked = 0;
1279
1280 ASSERT(RW_READ_HELD(&zv->zv_suspend_lock));
1281 ASSERT(MUTEX_HELD(&zv->zv_state_lock));
1282
1283 /*
1284 * In all other cases the spa_namespace_lock is taken before the
1285 * bdev->bd_mutex lock. But in this case the Linux __blkdev_get()
1286 * function calls fops->open() with the bdev->bd_mutex lock held.
1287 * This deadlock can be easily observed with zvols used as vdevs.
1288 *
1289 * To avoid a potential lock inversion deadlock we preemptively
1290 * try to take the spa_namespace_lock(). Normally it will not
1291 * be contended and this is safe because spa_open_common() handles
1292 * the case where the caller already holds the spa_namespace_lock.
1293 *
1294 * When it is contended we risk a lock inversion if we were to
1295 * block waiting for the lock. Luckily, the __blkdev_get()
1296 * function allows us to return -ERESTARTSYS which will result in
1297 * bdev->bd_mutex being dropped, reacquired, and fops->open() being
1298 * called again. This process can be repeated safely until both
1299 * locks are acquired.
1300 */
1301 if (!mutex_owned(&spa_namespace_lock)) {
1302 locked = mutex_tryenter(&spa_namespace_lock);
1303 if (!locked)
1304 return (-SET_ERROR(ERESTARTSYS));
1305 }
1306
1307 /* lie and say we're read-only */
1308 error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, 1, 1, zv, &os);
1309 if (error)
1310 goto out_mutex;
1311
1312 zv->zv_objset = os;
1313
1314 error = zvol_setup_zv(zv);
1315
1316 if (error) {
1317 dmu_objset_disown(os, 1, zv);
1318 zv->zv_objset = NULL;
1319 }
1320
1321 out_mutex:
1322 if (locked)
1323 mutex_exit(&spa_namespace_lock);
1324 return (SET_ERROR(-error));
1325 }
1326
1327 static void
1328 zvol_last_close(zvol_state_t *zv)
1329 {
1330 ASSERT(RW_READ_HELD(&zv->zv_suspend_lock));
1331 ASSERT(MUTEX_HELD(&zv->zv_state_lock));
1332
1333 zvol_shutdown_zv(zv);
1334
1335 dmu_objset_disown(zv->zv_objset, 1, zv);
1336 zv->zv_objset = NULL;
1337 }
1338
1339 static int
1340 zvol_open(struct block_device *bdev, fmode_t flag)
1341 {
1342 zvol_state_t *zv;
1343 int error = 0;
1344 boolean_t drop_suspend = B_TRUE;
1345
1346 ASSERT(!MUTEX_HELD(&zvol_state_lock));
1347
1348 mutex_enter(&zvol_state_lock);
1349 /*
1350 * Obtain a copy of private_data under the zvol_state_lock to make
1351 * sure that either the result of zvol free code path setting
1352 * bdev->bd_disk->private_data to NULL is observed, or zvol_free()
1353 * is not called on this zv because of the positive zv_open_count.
1354 */
1355 zv = bdev->bd_disk->private_data;
1356 if (zv == NULL) {
1357 mutex_exit(&zvol_state_lock);
1358 return (SET_ERROR(-ENXIO));
1359 }
1360
1361 mutex_enter(&zv->zv_state_lock);
1362 /*
1363 * make sure zvol is not suspended during first open
1364 * (hold zv_suspend_lock) and respect proper lock acquisition
1365 * ordering - zv_suspend_lock before zv_state_lock
1366 */
1367 if (zv->zv_open_count == 0) {
1368 if (!rw_tryenter(&zv->zv_suspend_lock, RW_READER)) {
1369 mutex_exit(&zv->zv_state_lock);
1370 rw_enter(&zv->zv_suspend_lock, RW_READER);
1371 mutex_enter(&zv->zv_state_lock);
1372 /* check to see if zv_suspend_lock is needed */
1373 if (zv->zv_open_count != 0) {
1374 rw_exit(&zv->zv_suspend_lock);
1375 drop_suspend = B_FALSE;
1376 }
1377 }
1378 } else {
1379 drop_suspend = B_FALSE;
1380 }
1381 mutex_exit(&zvol_state_lock);
1382
1383 ASSERT(MUTEX_HELD(&zv->zv_state_lock));
1384 ASSERT(zv->zv_open_count != 0 || RW_READ_HELD(&zv->zv_suspend_lock));
1385
1386 if (zv->zv_open_count == 0) {
1387 error = zvol_first_open(zv);
1388 if (error)
1389 goto out_mutex;
1390 }
1391
1392 if ((flag & FMODE_WRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
1393 error = -EROFS;
1394 goto out_open_count;
1395 }
1396
1397 zv->zv_open_count++;
1398
1399 check_disk_change(bdev);
1400
1401 out_open_count:
1402 if (zv->zv_open_count == 0)
1403 zvol_last_close(zv);
1404 out_mutex:
1405 mutex_exit(&zv->zv_state_lock);
1406 if (drop_suspend)
1407 rw_exit(&zv->zv_suspend_lock);
1408 if (error == -ERESTARTSYS)
1409 schedule();
1410
1411 return (SET_ERROR(error));
1412 }
1413
1414 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
1415 static void
1416 #else
1417 static int
1418 #endif
1419 zvol_release(struct gendisk *disk, fmode_t mode)
1420 {
1421 zvol_state_t *zv;
1422 boolean_t drop_suspend = B_TRUE;
1423
1424 ASSERT(!MUTEX_HELD(&zvol_state_lock));
1425
1426 mutex_enter(&zvol_state_lock);
1427 zv = disk->private_data;
1428
1429 mutex_enter(&zv->zv_state_lock);
1430 ASSERT(zv->zv_open_count > 0);
1431 /*
1432 * make sure zvol is not suspended during last close
1433 * (hold zv_suspend_lock) and respect proper lock acquisition
1434 * ordering - zv_suspend_lock before zv_state_lock
1435 */
1436 if (zv->zv_open_count == 1) {
1437 if (!rw_tryenter(&zv->zv_suspend_lock, RW_READER)) {
1438 mutex_exit(&zv->zv_state_lock);
1439 rw_enter(&zv->zv_suspend_lock, RW_READER);
1440 mutex_enter(&zv->zv_state_lock);
1441 /* check to see if zv_suspend_lock is needed */
1442 if (zv->zv_open_count != 1) {
1443 rw_exit(&zv->zv_suspend_lock);
1444 drop_suspend = B_FALSE;
1445 }
1446 }
1447 } else {
1448 drop_suspend = B_FALSE;
1449 }
1450 mutex_exit(&zvol_state_lock);
1451
1452 ASSERT(MUTEX_HELD(&zv->zv_state_lock));
1453 ASSERT(zv->zv_open_count != 1 || RW_READ_HELD(&zv->zv_suspend_lock));
1454
1455 zv->zv_open_count--;
1456 if (zv->zv_open_count == 0)
1457 zvol_last_close(zv);
1458
1459 mutex_exit(&zv->zv_state_lock);
1460
1461 if (drop_suspend)
1462 rw_exit(&zv->zv_suspend_lock);
1463
1464 #ifndef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
1465 return (0);
1466 #endif
1467 }
1468
1469 static int
1470 zvol_ioctl(struct block_device *bdev, fmode_t mode,
1471 unsigned int cmd, unsigned long arg)
1472 {
1473 zvol_state_t *zv = bdev->bd_disk->private_data;
1474 int error = 0;
1475
1476 ASSERT(zv && zv->zv_open_count > 0);
1477
1478 switch (cmd) {
1479 case BLKFLSBUF:
1480 fsync_bdev(bdev);
1481 invalidate_bdev(bdev);
1482 rw_enter(&zv->zv_suspend_lock, RW_READER);
1483
1484 if (dsl_dataset_is_dirty(dmu_objset_ds(zv->zv_objset)) &&
1485 !(zv->zv_flags & ZVOL_RDONLY))
1486 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
1487
1488 rw_exit(&zv->zv_suspend_lock);
1489 break;
1490
1491 case BLKZNAME:
1492 mutex_enter(&zv->zv_state_lock);
1493 error = copy_to_user((void *)arg, zv->zv_name, MAXNAMELEN);
1494 mutex_exit(&zv->zv_state_lock);
1495 break;
1496
1497 default:
1498 error = -ENOTTY;
1499 break;
1500 }
1501
1502 return (SET_ERROR(error));
1503 }
1504
1505 #ifdef CONFIG_COMPAT
1506 static int
1507 zvol_compat_ioctl(struct block_device *bdev, fmode_t mode,
1508 unsigned cmd, unsigned long arg)
1509 {
1510 return (zvol_ioctl(bdev, mode, cmd, arg));
1511 }
1512 #else
1513 #define zvol_compat_ioctl NULL
1514 #endif
1515
1516 static int zvol_media_changed(struct gendisk *disk)
1517 {
1518 zvol_state_t *zv = disk->private_data;
1519
1520 ASSERT(zv && zv->zv_open_count > 0);
1521
1522 return (zv->zv_changed);
1523 }
1524
1525 static int zvol_revalidate_disk(struct gendisk *disk)
1526 {
1527 zvol_state_t *zv = disk->private_data;
1528
1529 ASSERT(zv && zv->zv_open_count > 0);
1530
1531 zv->zv_changed = 0;
1532 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1533
1534 return (0);
1535 }
1536
1537 /*
1538 * Provide a simple virtual geometry for legacy compatibility. For devices
1539 * smaller than 1 MiB a small head and sector count is used to allow very
1540 * tiny devices. For devices over 1 Mib a standard head and sector count
1541 * is used to keep the cylinders count reasonable.
1542 */
1543 static int
1544 zvol_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1545 {
1546 zvol_state_t *zv = bdev->bd_disk->private_data;
1547 sector_t sectors;
1548
1549 ASSERT(zv && zv->zv_open_count > 0);
1550
1551 sectors = get_capacity(zv->zv_disk);
1552
1553 if (sectors > 2048) {
1554 geo->heads = 16;
1555 geo->sectors = 63;
1556 } else {
1557 geo->heads = 2;
1558 geo->sectors = 4;
1559 }
1560
1561 geo->start = 0;
1562 geo->cylinders = sectors / (geo->heads * geo->sectors);
1563
1564 return (0);
1565 }
1566
1567 static struct kobject *
1568 zvol_probe(dev_t dev, int *part, void *arg)
1569 {
1570 zvol_state_t *zv;
1571 struct kobject *kobj;
1572
1573 zv = zvol_find_by_dev(dev);
1574 kobj = zv ? get_disk(zv->zv_disk) : NULL;
1575 ASSERT(zv == NULL || MUTEX_HELD(&zv->zv_state_lock));
1576 if (zv)
1577 mutex_exit(&zv->zv_state_lock);
1578
1579 return (kobj);
1580 }
1581
1582 #ifdef HAVE_BDEV_BLOCK_DEVICE_OPERATIONS
1583 static struct block_device_operations zvol_ops = {
1584 .open = zvol_open,
1585 .release = zvol_release,
1586 .ioctl = zvol_ioctl,
1587 .compat_ioctl = zvol_compat_ioctl,
1588 .media_changed = zvol_media_changed,
1589 .revalidate_disk = zvol_revalidate_disk,
1590 .getgeo = zvol_getgeo,
1591 .owner = THIS_MODULE,
1592 };
1593
1594 #else /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1595
1596 static int
1597 zvol_open_by_inode(struct inode *inode, struct file *file)
1598 {
1599 return (zvol_open(inode->i_bdev, file->f_mode));
1600 }
1601
1602 static int
1603 zvol_release_by_inode(struct inode *inode, struct file *file)
1604 {
1605 return (zvol_release(inode->i_bdev->bd_disk, file->f_mode));
1606 }
1607
1608 static int
1609 zvol_ioctl_by_inode(struct inode *inode, struct file *file,
1610 unsigned int cmd, unsigned long arg)
1611 {
1612 if (file == NULL || inode == NULL)
1613 return (SET_ERROR(-EINVAL));
1614
1615 return (zvol_ioctl(inode->i_bdev, file->f_mode, cmd, arg));
1616 }
1617
1618 #ifdef CONFIG_COMPAT
1619 static long
1620 zvol_compat_ioctl_by_inode(struct file *file,
1621 unsigned int cmd, unsigned long arg)
1622 {
1623 if (file == NULL)
1624 return (SET_ERROR(-EINVAL));
1625
1626 return (zvol_compat_ioctl(file->f_dentry->d_inode->i_bdev,
1627 file->f_mode, cmd, arg));
1628 }
1629 #else
1630 #define zvol_compat_ioctl_by_inode NULL
1631 #endif
1632
1633 static struct block_device_operations zvol_ops = {
1634 .open = zvol_open_by_inode,
1635 .release = zvol_release_by_inode,
1636 .ioctl = zvol_ioctl_by_inode,
1637 .compat_ioctl = zvol_compat_ioctl_by_inode,
1638 .media_changed = zvol_media_changed,
1639 .revalidate_disk = zvol_revalidate_disk,
1640 .getgeo = zvol_getgeo,
1641 .owner = THIS_MODULE,
1642 };
1643 #endif /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1644
1645 /*
1646 * Allocate memory for a new zvol_state_t and setup the required
1647 * request queue and generic disk structures for the block device.
1648 */
1649 static zvol_state_t *
1650 zvol_alloc(dev_t dev, const char *name)
1651 {
1652 zvol_state_t *zv;
1653 uint64_t volmode;
1654
1655 if (dsl_prop_get_integer(name, "volmode", &volmode, NULL) != 0)
1656 return (NULL);
1657
1658 if (volmode == ZFS_VOLMODE_DEFAULT)
1659 volmode = zvol_volmode;
1660
1661 if (volmode == ZFS_VOLMODE_NONE)
1662 return (NULL);
1663
1664 zv = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
1665
1666 list_link_init(&zv->zv_next);
1667
1668 mutex_init(&zv->zv_state_lock, NULL, MUTEX_DEFAULT, NULL);
1669
1670 zv->zv_queue = blk_alloc_queue(GFP_ATOMIC);
1671 if (zv->zv_queue == NULL)
1672 goto out_kmem;
1673
1674 blk_queue_make_request(zv->zv_queue, zvol_request);
1675 blk_queue_set_write_cache(zv->zv_queue, B_TRUE, B_TRUE);
1676
1677 /* Limit read-ahead to a single page to prevent over-prefetching. */
1678 blk_queue_set_read_ahead(zv->zv_queue, 1);
1679
1680 /* Disable write merging in favor of the ZIO pipeline. */
1681 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, zv->zv_queue);
1682
1683 zv->zv_disk = alloc_disk(ZVOL_MINORS);
1684 if (zv->zv_disk == NULL)
1685 goto out_queue;
1686
1687 zv->zv_queue->queuedata = zv;
1688 zv->zv_dev = dev;
1689 zv->zv_open_count = 0;
1690 strlcpy(zv->zv_name, name, MAXNAMELEN);
1691
1692 zfs_rlock_init(&zv->zv_range_lock);
1693 rw_init(&zv->zv_suspend_lock, NULL, RW_DEFAULT, NULL);
1694
1695 zv->zv_disk->major = zvol_major;
1696 if (volmode == ZFS_VOLMODE_DEV) {
1697 /*
1698 * ZFS_VOLMODE_DEV disable partitioning on ZVOL devices: set
1699 * gendisk->minors = 1 as noted in include/linux/genhd.h.
1700 * Also disable extended partition numbers (GENHD_FL_EXT_DEVT)
1701 * and suppresses partition scanning (GENHD_FL_NO_PART_SCAN)
1702 * setting gendisk->flags accordingly.
1703 */
1704 zv->zv_disk->minors = 1;
1705 #if defined(GENHD_FL_EXT_DEVT)
1706 zv->zv_disk->flags &= ~GENHD_FL_EXT_DEVT;
1707 #endif
1708 #if defined(GENHD_FL_NO_PART_SCAN)
1709 zv->zv_disk->flags |= GENHD_FL_NO_PART_SCAN;
1710 #endif
1711 }
1712 zv->zv_disk->first_minor = (dev & MINORMASK);
1713 zv->zv_disk->fops = &zvol_ops;
1714 zv->zv_disk->private_data = zv;
1715 zv->zv_disk->queue = zv->zv_queue;
1716 snprintf(zv->zv_disk->disk_name, DISK_NAME_LEN, "%s%d",
1717 ZVOL_DEV_NAME, (dev & MINORMASK));
1718
1719 return (zv);
1720
1721 out_queue:
1722 blk_cleanup_queue(zv->zv_queue);
1723 out_kmem:
1724 kmem_free(zv, sizeof (zvol_state_t));
1725
1726 return (NULL);
1727 }
1728
1729 /*
1730 * Cleanup then free a zvol_state_t which was created by zvol_alloc().
1731 * At this time, the structure is not opened by anyone, is taken off
1732 * the zvol_state_list, and has its private data set to NULL.
1733 * The zvol_state_lock is dropped.
1734 */
1735 static void
1736 zvol_free(void *arg)
1737 {
1738 zvol_state_t *zv = arg;
1739
1740 ASSERT(!MUTEX_HELD(&zvol_state_lock));
1741 ASSERT(!RW_LOCK_HELD(&zv->zv_suspend_lock));
1742 ASSERT(!MUTEX_HELD(&zv->zv_state_lock));
1743 ASSERT(zv->zv_open_count == 0);
1744 ASSERT(zv->zv_disk->private_data == NULL);
1745
1746 rw_destroy(&zv->zv_suspend_lock);
1747 zfs_rlock_destroy(&zv->zv_range_lock);
1748
1749 del_gendisk(zv->zv_disk);
1750 blk_cleanup_queue(zv->zv_queue);
1751 put_disk(zv->zv_disk);
1752
1753 ida_simple_remove(&zvol_ida, MINOR(zv->zv_dev) >> ZVOL_MINOR_BITS);
1754
1755 mutex_destroy(&zv->zv_state_lock);
1756
1757 kmem_free(zv, sizeof (zvol_state_t));
1758 }
1759
1760 /*
1761 * Create a block device minor node and setup the linkage between it
1762 * and the specified volume. Once this function returns the block
1763 * device is live and ready for use.
1764 */
1765 static int
1766 zvol_create_minor_impl(const char *name)
1767 {
1768 zvol_state_t *zv;
1769 objset_t *os;
1770 dmu_object_info_t *doi;
1771 uint64_t volsize;
1772 uint64_t len;
1773 unsigned minor = 0;
1774 int error = 0;
1775 int idx;
1776 uint64_t hash = zvol_name_hash(name);
1777
1778 if (zvol_inhibit_dev)
1779 return (0);
1780
1781 idx = ida_simple_get(&zvol_ida, 0, 0, kmem_flags_convert(KM_SLEEP));
1782 if (idx < 0)
1783 return (SET_ERROR(-idx));
1784 minor = idx << ZVOL_MINOR_BITS;
1785
1786 zv = zvol_find_by_name_hash(name, hash, RW_NONE);
1787 if (zv) {
1788 ASSERT(MUTEX_HELD(&zv->zv_state_lock));
1789 mutex_exit(&zv->zv_state_lock);
1790 ida_simple_remove(&zvol_ida, idx);
1791 return (SET_ERROR(EEXIST));
1792 }
1793
1794 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
1795
1796 error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, B_TRUE, FTAG, &os);
1797 if (error)
1798 goto out_doi;
1799
1800 error = dmu_object_info(os, ZVOL_OBJ, doi);
1801 if (error)
1802 goto out_dmu_objset_disown;
1803
1804 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
1805 if (error)
1806 goto out_dmu_objset_disown;
1807
1808 zv = zvol_alloc(MKDEV(zvol_major, minor), name);
1809 if (zv == NULL) {
1810 error = SET_ERROR(EAGAIN);
1811 goto out_dmu_objset_disown;
1812 }
1813 zv->zv_hash = hash;
1814
1815 if (dmu_objset_is_snapshot(os))
1816 zv->zv_flags |= ZVOL_RDONLY;
1817
1818 zv->zv_volblocksize = doi->doi_data_block_size;
1819 zv->zv_volsize = volsize;
1820 zv->zv_objset = os;
1821
1822 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1823
1824 blk_queue_max_hw_sectors(zv->zv_queue, (DMU_MAX_ACCESS / 4) >> 9);
1825 blk_queue_max_segments(zv->zv_queue, UINT16_MAX);
1826 blk_queue_max_segment_size(zv->zv_queue, UINT_MAX);
1827 blk_queue_physical_block_size(zv->zv_queue, zv->zv_volblocksize);
1828 blk_queue_io_opt(zv->zv_queue, zv->zv_volblocksize);
1829 blk_queue_max_discard_sectors(zv->zv_queue,
1830 (zvol_max_discard_blocks * zv->zv_volblocksize) >> 9);
1831 blk_queue_discard_granularity(zv->zv_queue, zv->zv_volblocksize);
1832 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, zv->zv_queue);
1833 #ifdef QUEUE_FLAG_NONROT
1834 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zv->zv_queue);
1835 #endif
1836 #ifdef QUEUE_FLAG_ADD_RANDOM
1837 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, zv->zv_queue);
1838 #endif
1839
1840 if (spa_writeable(dmu_objset_spa(os))) {
1841 if (zil_replay_disable)
1842 zil_destroy(dmu_objset_zil(os), B_FALSE);
1843 else
1844 zil_replay(os, zv, zvol_replay_vector);
1845 }
1846
1847 /*
1848 * When udev detects the addition of the device it will immediately
1849 * invoke blkid(8) to determine the type of content on the device.
1850 * Prefetching the blocks commonly scanned by blkid(8) will speed
1851 * up this process.
1852 */
1853 len = MIN(MAX(zvol_prefetch_bytes, 0), SPA_MAXBLOCKSIZE);
1854 if (len > 0) {
1855 dmu_prefetch(os, ZVOL_OBJ, 0, 0, len, ZIO_PRIORITY_SYNC_READ);
1856 dmu_prefetch(os, ZVOL_OBJ, 0, volsize - len, len,
1857 ZIO_PRIORITY_SYNC_READ);
1858 }
1859
1860 zv->zv_objset = NULL;
1861 out_dmu_objset_disown:
1862 dmu_objset_disown(os, B_TRUE, FTAG);
1863 out_doi:
1864 kmem_free(doi, sizeof (dmu_object_info_t));
1865
1866 if (error == 0) {
1867 mutex_enter(&zvol_state_lock);
1868 zvol_insert(zv);
1869 mutex_exit(&zvol_state_lock);
1870 add_disk(zv->zv_disk);
1871 } else {
1872 ida_simple_remove(&zvol_ida, idx);
1873 }
1874
1875 return (SET_ERROR(error));
1876 }
1877
1878 /*
1879 * Rename a block device minor mode for the specified volume.
1880 */
1881 static void
1882 zvol_rename_minor(zvol_state_t *zv, const char *newname)
1883 {
1884 int readonly = get_disk_ro(zv->zv_disk);
1885
1886 ASSERT(MUTEX_HELD(&zvol_state_lock));
1887 ASSERT(MUTEX_HELD(&zv->zv_state_lock));
1888
1889 strlcpy(zv->zv_name, newname, sizeof (zv->zv_name));
1890
1891 /* move to new hashtable entry */
1892 zv->zv_hash = zvol_name_hash(zv->zv_name);
1893 hlist_del(&zv->zv_hlink);
1894 hlist_add_head(&zv->zv_hlink, ZVOL_HT_HEAD(zv->zv_hash));
1895
1896 /*
1897 * The block device's read-only state is briefly changed causing
1898 * a KOBJ_CHANGE uevent to be issued. This ensures udev detects
1899 * the name change and fixes the symlinks. This does not change
1900 * ZVOL_RDONLY in zv->zv_flags so the actual read-only state never
1901 * changes. This would normally be done using kobject_uevent() but
1902 * that is a GPL-only symbol which is why we need this workaround.
1903 */
1904 set_disk_ro(zv->zv_disk, !readonly);
1905 set_disk_ro(zv->zv_disk, readonly);
1906 }
1907
1908 typedef struct minors_job {
1909 list_t *list;
1910 list_node_t link;
1911 /* input */
1912 char *name;
1913 /* output */
1914 int error;
1915 } minors_job_t;
1916
1917 /*
1918 * Prefetch zvol dnodes for the minors_job
1919 */
1920 static void
1921 zvol_prefetch_minors_impl(void *arg)
1922 {
1923 minors_job_t *job = arg;
1924 char *dsname = job->name;
1925 objset_t *os = NULL;
1926
1927 job->error = dmu_objset_own(dsname, DMU_OST_ZVOL, B_TRUE, B_TRUE,
1928 FTAG, &os);
1929 if (job->error == 0) {
1930 dmu_prefetch(os, ZVOL_OBJ, 0, 0, 0, ZIO_PRIORITY_SYNC_READ);
1931 dmu_objset_disown(os, B_TRUE, FTAG);
1932 }
1933 }
1934
1935 /*
1936 * Mask errors to continue dmu_objset_find() traversal
1937 */
1938 static int
1939 zvol_create_snap_minor_cb(const char *dsname, void *arg)
1940 {
1941 minors_job_t *j = arg;
1942 list_t *minors_list = j->list;
1943 const char *name = j->name;
1944
1945 ASSERT0(MUTEX_HELD(&spa_namespace_lock));
1946
1947 /* skip the designated dataset */
1948 if (name && strcmp(dsname, name) == 0)
1949 return (0);
1950
1951 /* at this point, the dsname should name a snapshot */
1952 if (strchr(dsname, '@') == 0) {
1953 dprintf("zvol_create_snap_minor_cb(): "
1954 "%s is not a shapshot name\n", dsname);
1955 } else {
1956 minors_job_t *job;
1957 char *n = strdup(dsname);
1958 if (n == NULL)
1959 return (0);
1960
1961 job = kmem_alloc(sizeof (minors_job_t), KM_SLEEP);
1962 job->name = n;
1963 job->list = minors_list;
1964 job->error = 0;
1965 list_insert_tail(minors_list, job);
1966 /* don't care if dispatch fails, because job->error is 0 */
1967 taskq_dispatch(system_taskq, zvol_prefetch_minors_impl, job,
1968 TQ_SLEEP);
1969 }
1970
1971 return (0);
1972 }
1973
1974 /*
1975 * Mask errors to continue dmu_objset_find() traversal
1976 */
1977 static int
1978 zvol_create_minors_cb(const char *dsname, void *arg)
1979 {
1980 uint64_t snapdev;
1981 int error;
1982 list_t *minors_list = arg;
1983
1984 ASSERT0(MUTEX_HELD(&spa_namespace_lock));
1985
1986 error = dsl_prop_get_integer(dsname, "snapdev", &snapdev, NULL);
1987 if (error)
1988 return (0);
1989
1990 /*
1991 * Given the name and the 'snapdev' property, create device minor nodes
1992 * with the linkages to zvols/snapshots as needed.
1993 * If the name represents a zvol, create a minor node for the zvol, then
1994 * check if its snapshots are 'visible', and if so, iterate over the
1995 * snapshots and create device minor nodes for those.
1996 */
1997 if (strchr(dsname, '@') == 0) {
1998 minors_job_t *job;
1999 char *n = strdup(dsname);
2000 if (n == NULL)
2001 return (0);
2002
2003 job = kmem_alloc(sizeof (minors_job_t), KM_SLEEP);
2004 job->name = n;
2005 job->list = minors_list;
2006 job->error = 0;
2007 list_insert_tail(minors_list, job);
2008 /* don't care if dispatch fails, because job->error is 0 */
2009 taskq_dispatch(system_taskq, zvol_prefetch_minors_impl, job,
2010 TQ_SLEEP);
2011
2012 if (snapdev == ZFS_SNAPDEV_VISIBLE) {
2013 /*
2014 * traverse snapshots only, do not traverse children,
2015 * and skip the 'dsname'
2016 */
2017 error = dmu_objset_find((char *)dsname,
2018 zvol_create_snap_minor_cb, (void *)job,
2019 DS_FIND_SNAPSHOTS);
2020 }
2021 } else {
2022 dprintf("zvol_create_minors_cb(): %s is not a zvol name\n",
2023 dsname);
2024 }
2025
2026 return (0);
2027 }
2028
2029 /*
2030 * Create minors for the specified dataset, including children and snapshots.
2031 * Pay attention to the 'snapdev' property and iterate over the snapshots
2032 * only if they are 'visible'. This approach allows one to assure that the
2033 * snapshot metadata is read from disk only if it is needed.
2034 *
2035 * The name can represent a dataset to be recursively scanned for zvols and
2036 * their snapshots, or a single zvol snapshot. If the name represents a
2037 * dataset, the scan is performed in two nested stages:
2038 * - scan the dataset for zvols, and
2039 * - for each zvol, create a minor node, then check if the zvol's snapshots
2040 * are 'visible', and only then iterate over the snapshots if needed
2041 *
2042 * If the name represents a snapshot, a check is performed if the snapshot is
2043 * 'visible' (which also verifies that the parent is a zvol), and if so,
2044 * a minor node for that snapshot is created.
2045 */
2046 static int
2047 zvol_create_minors_impl(const char *name)
2048 {
2049 int error = 0;
2050 fstrans_cookie_t cookie;
2051 char *atp, *parent;
2052 list_t minors_list;
2053 minors_job_t *job;
2054
2055 if (zvol_inhibit_dev)
2056 return (0);
2057
2058 /*
2059 * This is the list for prefetch jobs. Whenever we found a match
2060 * during dmu_objset_find, we insert a minors_job to the list and do
2061 * taskq_dispatch to parallel prefetch zvol dnodes. Note we don't need
2062 * any lock because all list operation is done on the current thread.
2063 *
2064 * We will use this list to do zvol_create_minor_impl after prefetch
2065 * so we don't have to traverse using dmu_objset_find again.
2066 */
2067 list_create(&minors_list, sizeof (minors_job_t),
2068 offsetof(minors_job_t, link));
2069
2070 parent = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2071 (void) strlcpy(parent, name, MAXPATHLEN);
2072
2073 if ((atp = strrchr(parent, '@')) != NULL) {
2074 uint64_t snapdev;
2075
2076 *atp = '\0';
2077 error = dsl_prop_get_integer(parent, "snapdev",
2078 &snapdev, NULL);
2079
2080 if (error == 0 && snapdev == ZFS_SNAPDEV_VISIBLE)
2081 error = zvol_create_minor_impl(name);
2082 } else {
2083 cookie = spl_fstrans_mark();
2084 error = dmu_objset_find(parent, zvol_create_minors_cb,
2085 &minors_list, DS_FIND_CHILDREN);
2086 spl_fstrans_unmark(cookie);
2087 }
2088
2089 kmem_free(parent, MAXPATHLEN);
2090 taskq_wait_outstanding(system_taskq, 0);
2091
2092 /*
2093 * Prefetch is completed, we can do zvol_create_minor_impl
2094 * sequentially.
2095 */
2096 while ((job = list_head(&minors_list)) != NULL) {
2097 list_remove(&minors_list, job);
2098 if (!job->error)
2099 zvol_create_minor_impl(job->name);
2100 strfree(job->name);
2101 kmem_free(job, sizeof (minors_job_t));
2102 }
2103
2104 list_destroy(&minors_list);
2105
2106 return (SET_ERROR(error));
2107 }
2108
2109 /*
2110 * Remove minors for specified dataset including children and snapshots.
2111 */
2112 static void
2113 zvol_remove_minors_impl(const char *name)
2114 {
2115 zvol_state_t *zv, *zv_next;
2116 int namelen = ((name) ? strlen(name) : 0);
2117 taskqid_t t, tid = TASKQID_INVALID;
2118 list_t free_list;
2119
2120 if (zvol_inhibit_dev)
2121 return;
2122
2123 list_create(&free_list, sizeof (zvol_state_t),
2124 offsetof(zvol_state_t, zv_next));
2125
2126 mutex_enter(&zvol_state_lock);
2127
2128 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
2129 zv_next = list_next(&zvol_state_list, zv);
2130
2131 mutex_enter(&zv->zv_state_lock);
2132 if (name == NULL || strcmp(zv->zv_name, name) == 0 ||
2133 (strncmp(zv->zv_name, name, namelen) == 0 &&
2134 (zv->zv_name[namelen] == '/' ||
2135 zv->zv_name[namelen] == '@'))) {
2136 /*
2137 * By holding zv_state_lock here, we guarantee that no
2138 * one is currently using this zv
2139 */
2140
2141 /* If in use, leave alone */
2142 if (zv->zv_open_count > 0 ||
2143 atomic_read(&zv->zv_suspend_ref)) {
2144 mutex_exit(&zv->zv_state_lock);
2145 continue;
2146 }
2147
2148 zvol_remove(zv);
2149
2150 /*
2151 * clear this while holding zvol_state_lock so
2152 * zvol_open won't open it
2153 */
2154 zv->zv_disk->private_data = NULL;
2155
2156 /* Drop zv_state_lock before zvol_free() */
2157 mutex_exit(&zv->zv_state_lock);
2158
2159 /* try parallel zv_free, if failed do it in place */
2160 t = taskq_dispatch(system_taskq, zvol_free, zv,
2161 TQ_SLEEP);
2162 if (t == TASKQID_INVALID)
2163 list_insert_head(&free_list, zv);
2164 else
2165 tid = t;
2166 } else {
2167 mutex_exit(&zv->zv_state_lock);
2168 }
2169 }
2170 mutex_exit(&zvol_state_lock);
2171
2172 /*
2173 * Drop zvol_state_lock before calling zvol_free()
2174 */
2175 while ((zv = list_head(&free_list)) != NULL) {
2176 list_remove(&free_list, zv);
2177 zvol_free(zv);
2178 }
2179
2180 if (tid != TASKQID_INVALID)
2181 taskq_wait_outstanding(system_taskq, tid);
2182 }
2183
2184 /* Remove minor for this specific volume only */
2185 static void
2186 zvol_remove_minor_impl(const char *name)
2187 {
2188 zvol_state_t *zv = NULL, *zv_next;
2189
2190 if (zvol_inhibit_dev)
2191 return;
2192
2193 mutex_enter(&zvol_state_lock);
2194
2195 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
2196 zv_next = list_next(&zvol_state_list, zv);
2197
2198 mutex_enter(&zv->zv_state_lock);
2199 if (strcmp(zv->zv_name, name) == 0) {
2200 /*
2201 * By holding zv_state_lock here, we guarantee that no
2202 * one is currently using this zv
2203 */
2204
2205 /* If in use, leave alone */
2206 if (zv->zv_open_count > 0 ||
2207 atomic_read(&zv->zv_suspend_ref)) {
2208 mutex_exit(&zv->zv_state_lock);
2209 continue;
2210 }
2211 zvol_remove(zv);
2212
2213 /* clear this so zvol_open won't open it */
2214 zv->zv_disk->private_data = NULL;
2215
2216 mutex_exit(&zv->zv_state_lock);
2217 break;
2218 } else {
2219 mutex_exit(&zv->zv_state_lock);
2220 }
2221 }
2222
2223 /* Drop zvol_state_lock before calling zvol_free() */
2224 mutex_exit(&zvol_state_lock);
2225
2226 if (zv != NULL)
2227 zvol_free(zv);
2228 }
2229
2230 /*
2231 * Rename minors for specified dataset including children and snapshots.
2232 */
2233 static void
2234 zvol_rename_minors_impl(const char *oldname, const char *newname)
2235 {
2236 zvol_state_t *zv, *zv_next;
2237 int oldnamelen, newnamelen;
2238
2239 if (zvol_inhibit_dev)
2240 return;
2241
2242 oldnamelen = strlen(oldname);
2243 newnamelen = strlen(newname);
2244
2245 mutex_enter(&zvol_state_lock);
2246
2247 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
2248 zv_next = list_next(&zvol_state_list, zv);
2249
2250 mutex_enter(&zv->zv_state_lock);
2251
2252 /* If in use, leave alone */
2253 if (zv->zv_open_count > 0) {
2254 mutex_exit(&zv->zv_state_lock);
2255 continue;
2256 }
2257
2258 if (strcmp(zv->zv_name, oldname) == 0) {
2259 zvol_rename_minor(zv, newname);
2260 } else if (strncmp(zv->zv_name, oldname, oldnamelen) == 0 &&
2261 (zv->zv_name[oldnamelen] == '/' ||
2262 zv->zv_name[oldnamelen] == '@')) {
2263 char *name = kmem_asprintf("%s%c%s", newname,
2264 zv->zv_name[oldnamelen],
2265 zv->zv_name + oldnamelen + 1);
2266 zvol_rename_minor(zv, name);
2267 kmem_free(name, strlen(name + 1));
2268 }
2269
2270 mutex_exit(&zv->zv_state_lock);
2271 }
2272
2273 mutex_exit(&zvol_state_lock);
2274 }
2275
2276 typedef struct zvol_snapdev_cb_arg {
2277 uint64_t snapdev;
2278 } zvol_snapdev_cb_arg_t;
2279
2280 static int
2281 zvol_set_snapdev_cb(const char *dsname, void *param)
2282 {
2283 zvol_snapdev_cb_arg_t *arg = param;
2284
2285 if (strchr(dsname, '@') == NULL)
2286 return (0);
2287
2288 switch (arg->snapdev) {
2289 case ZFS_SNAPDEV_VISIBLE:
2290 (void) zvol_create_minor_impl(dsname);
2291 break;
2292 case ZFS_SNAPDEV_HIDDEN:
2293 (void) zvol_remove_minor_impl(dsname);
2294 break;
2295 }
2296
2297 return (0);
2298 }
2299
2300 static void
2301 zvol_set_snapdev_impl(char *name, uint64_t snapdev)
2302 {
2303 zvol_snapdev_cb_arg_t arg = {snapdev};
2304 fstrans_cookie_t cookie = spl_fstrans_mark();
2305 /*
2306 * The zvol_set_snapdev_sync() sets snapdev appropriately
2307 * in the dataset hierarchy. Here, we only scan snapshots.
2308 */
2309 dmu_objset_find(name, zvol_set_snapdev_cb, &arg, DS_FIND_SNAPSHOTS);
2310 spl_fstrans_unmark(cookie);
2311 }
2312
2313 typedef struct zvol_volmode_cb_arg {
2314 uint64_t volmode;
2315 } zvol_volmode_cb_arg_t;
2316
2317 static void
2318 zvol_set_volmode_impl(char *name, uint64_t volmode)
2319 {
2320 fstrans_cookie_t cookie = spl_fstrans_mark();
2321
2322 if (strchr(name, '@') != NULL)
2323 return;
2324
2325 /*
2326 * It's unfortunate we need to remove minors before we create new ones:
2327 * this is necessary because our backing gendisk (zvol_state->zv_disk)
2328 * coule be different when we set, for instance, volmode from "geom"
2329 * to "dev" (or vice versa).
2330 * A possible optimization is to modify our consumers so we don't get
2331 * called when "volmode" does not change.
2332 */
2333 switch (volmode) {
2334 case ZFS_VOLMODE_NONE:
2335 (void) zvol_remove_minor_impl(name);
2336 break;
2337 case ZFS_VOLMODE_GEOM:
2338 case ZFS_VOLMODE_DEV:
2339 (void) zvol_remove_minor_impl(name);
2340 (void) zvol_create_minor_impl(name);
2341 break;
2342 case ZFS_VOLMODE_DEFAULT:
2343 (void) zvol_remove_minor_impl(name);
2344 if (zvol_volmode == ZFS_VOLMODE_NONE)
2345 break;
2346 else /* if zvol_volmode is invalid defaults to "geom" */
2347 (void) zvol_create_minor_impl(name);
2348 break;
2349 }
2350
2351 spl_fstrans_unmark(cookie);
2352 }
2353
2354 static zvol_task_t *
2355 zvol_task_alloc(zvol_async_op_t op, const char *name1, const char *name2,
2356 uint64_t value)
2357 {
2358 zvol_task_t *task;
2359 char *delim;
2360
2361 /* Never allow tasks on hidden names. */
2362 if (name1[0] == '$')
2363 return (NULL);
2364
2365 task = kmem_zalloc(sizeof (zvol_task_t), KM_SLEEP);
2366 task->op = op;
2367 task->value = value;
2368 delim = strchr(name1, '/');
2369 strlcpy(task->pool, name1, delim ? (delim - name1 + 1) : MAXNAMELEN);
2370
2371 strlcpy(task->name1, name1, MAXNAMELEN);
2372 if (name2 != NULL)
2373 strlcpy(task->name2, name2, MAXNAMELEN);
2374
2375 return (task);
2376 }
2377
2378 static void
2379 zvol_task_free(zvol_task_t *task)
2380 {
2381 kmem_free(task, sizeof (zvol_task_t));
2382 }
2383
2384 /*
2385 * The worker thread function performed asynchronously.
2386 */
2387 static void
2388 zvol_task_cb(void *param)
2389 {
2390 zvol_task_t *task = (zvol_task_t *)param;
2391
2392 switch (task->op) {
2393 case ZVOL_ASYNC_CREATE_MINORS:
2394 (void) zvol_create_minors_impl(task->name1);
2395 break;
2396 case ZVOL_ASYNC_REMOVE_MINORS:
2397 zvol_remove_minors_impl(task->name1);
2398 break;
2399 case ZVOL_ASYNC_RENAME_MINORS:
2400 zvol_rename_minors_impl(task->name1, task->name2);
2401 break;
2402 case ZVOL_ASYNC_SET_SNAPDEV:
2403 zvol_set_snapdev_impl(task->name1, task->value);
2404 break;
2405 case ZVOL_ASYNC_SET_VOLMODE:
2406 zvol_set_volmode_impl(task->name1, task->value);
2407 break;
2408 default:
2409 VERIFY(0);
2410 break;
2411 }
2412
2413 zvol_task_free(task);
2414 }
2415
2416 typedef struct zvol_set_prop_int_arg {
2417 const char *zsda_name;
2418 uint64_t zsda_value;
2419 zprop_source_t zsda_source;
2420 dmu_tx_t *zsda_tx;
2421 } zvol_set_prop_int_arg_t;
2422
2423 /*
2424 * Sanity check the dataset for safe use by the sync task. No additional
2425 * conditions are imposed.
2426 */
2427 static int
2428 zvol_set_snapdev_check(void *arg, dmu_tx_t *tx)
2429 {
2430 zvol_set_prop_int_arg_t *zsda = arg;
2431 dsl_pool_t *dp = dmu_tx_pool(tx);
2432 dsl_dir_t *dd;
2433 int error;
2434
2435 error = dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL);
2436 if (error != 0)
2437 return (error);
2438
2439 dsl_dir_rele(dd, FTAG);
2440
2441 return (error);
2442 }
2443
2444 /* ARGSUSED */
2445 static int
2446 zvol_set_snapdev_sync_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
2447 {
2448 char dsname[MAXNAMELEN];
2449 zvol_task_t *task;
2450 uint64_t snapdev;
2451
2452 dsl_dataset_name(ds, dsname);
2453 if (dsl_prop_get_int_ds(ds, "snapdev", &snapdev) != 0)
2454 return (0);
2455 task = zvol_task_alloc(ZVOL_ASYNC_SET_SNAPDEV, dsname, NULL, snapdev);
2456 if (task == NULL)
2457 return (0);
2458
2459 (void) taskq_dispatch(dp->dp_spa->spa_zvol_taskq, zvol_task_cb,
2460 task, TQ_SLEEP);
2461 return (0);
2462 }
2463
2464 /*
2465 * Traverse all child datasets and apply snapdev appropriately.
2466 * We call dsl_prop_set_sync_impl() here to set the value only on the toplevel
2467 * dataset and read the effective "snapdev" on every child in the callback
2468 * function: this is because the value is not guaranteed to be the same in the
2469 * whole dataset hierarchy.
2470 */
2471 static void
2472 zvol_set_snapdev_sync(void *arg, dmu_tx_t *tx)
2473 {
2474 zvol_set_prop_int_arg_t *zsda = arg;
2475 dsl_pool_t *dp = dmu_tx_pool(tx);
2476 dsl_dir_t *dd;
2477 dsl_dataset_t *ds;
2478 int error;
2479
2480 VERIFY0(dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL));
2481 zsda->zsda_tx = tx;
2482
2483 error = dsl_dataset_hold(dp, zsda->zsda_name, FTAG, &ds);
2484 if (error == 0) {
2485 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_SNAPDEV),
2486 zsda->zsda_source, sizeof (zsda->zsda_value), 1,
2487 &zsda->zsda_value, zsda->zsda_tx);
2488 dsl_dataset_rele(ds, FTAG);
2489 }
2490 dmu_objset_find_dp(dp, dd->dd_object, zvol_set_snapdev_sync_cb,
2491 zsda, DS_FIND_CHILDREN);
2492
2493 dsl_dir_rele(dd, FTAG);
2494 }
2495
2496 int
2497 zvol_set_snapdev(const char *ddname, zprop_source_t source, uint64_t snapdev)
2498 {
2499 zvol_set_prop_int_arg_t zsda;
2500
2501 zsda.zsda_name = ddname;
2502 zsda.zsda_source = source;
2503 zsda.zsda_value = snapdev;
2504
2505 return (dsl_sync_task(ddname, zvol_set_snapdev_check,
2506 zvol_set_snapdev_sync, &zsda, 0, ZFS_SPACE_CHECK_NONE));
2507 }
2508
2509 /*
2510 * Sanity check the dataset for safe use by the sync task. No additional
2511 * conditions are imposed.
2512 */
2513 static int
2514 zvol_set_volmode_check(void *arg, dmu_tx_t *tx)
2515 {
2516 zvol_set_prop_int_arg_t *zsda = arg;
2517 dsl_pool_t *dp = dmu_tx_pool(tx);
2518 dsl_dir_t *dd;
2519 int error;
2520
2521 error = dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL);
2522 if (error != 0)
2523 return (error);
2524
2525 dsl_dir_rele(dd, FTAG);
2526
2527 return (error);
2528 }
2529
2530 /* ARGSUSED */
2531 static int
2532 zvol_set_volmode_sync_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
2533 {
2534 char dsname[MAXNAMELEN];
2535 zvol_task_t *task;
2536 uint64_t volmode;
2537
2538 dsl_dataset_name(ds, dsname);
2539 if (dsl_prop_get_int_ds(ds, "volmode", &volmode) != 0)
2540 return (0);
2541 task = zvol_task_alloc(ZVOL_ASYNC_SET_VOLMODE, dsname, NULL, volmode);
2542 if (task == NULL)
2543 return (0);
2544
2545 (void) taskq_dispatch(dp->dp_spa->spa_zvol_taskq, zvol_task_cb,
2546 task, TQ_SLEEP);
2547 return (0);
2548 }
2549
2550 /*
2551 * Traverse all child datasets and apply volmode appropriately.
2552 * We call dsl_prop_set_sync_impl() here to set the value only on the toplevel
2553 * dataset and read the effective "volmode" on every child in the callback
2554 * function: this is because the value is not guaranteed to be the same in the
2555 * whole dataset hierarchy.
2556 */
2557 static void
2558 zvol_set_volmode_sync(void *arg, dmu_tx_t *tx)
2559 {
2560 zvol_set_prop_int_arg_t *zsda = arg;
2561 dsl_pool_t *dp = dmu_tx_pool(tx);
2562 dsl_dir_t *dd;
2563 dsl_dataset_t *ds;
2564 int error;
2565
2566 VERIFY0(dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL));
2567 zsda->zsda_tx = tx;
2568
2569 error = dsl_dataset_hold(dp, zsda->zsda_name, FTAG, &ds);
2570 if (error == 0) {
2571 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_VOLMODE),
2572 zsda->zsda_source, sizeof (zsda->zsda_value), 1,
2573 &zsda->zsda_value, zsda->zsda_tx);
2574 dsl_dataset_rele(ds, FTAG);
2575 }
2576
2577 dmu_objset_find_dp(dp, dd->dd_object, zvol_set_volmode_sync_cb,
2578 zsda, DS_FIND_CHILDREN);
2579
2580 dsl_dir_rele(dd, FTAG);
2581 }
2582
2583 int
2584 zvol_set_volmode(const char *ddname, zprop_source_t source, uint64_t volmode)
2585 {
2586 zvol_set_prop_int_arg_t zsda;
2587
2588 zsda.zsda_name = ddname;
2589 zsda.zsda_source = source;
2590 zsda.zsda_value = volmode;
2591
2592 return (dsl_sync_task(ddname, zvol_set_volmode_check,
2593 zvol_set_volmode_sync, &zsda, 0, ZFS_SPACE_CHECK_NONE));
2594 }
2595
2596 void
2597 zvol_create_minors(spa_t *spa, const char *name, boolean_t async)
2598 {
2599 zvol_task_t *task;
2600 taskqid_t id;
2601
2602 task = zvol_task_alloc(ZVOL_ASYNC_CREATE_MINORS, name, NULL, ~0ULL);
2603 if (task == NULL)
2604 return;
2605
2606 id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP);
2607 if ((async == B_FALSE) && (id != TASKQID_INVALID))
2608 taskq_wait_id(spa->spa_zvol_taskq, id);
2609 }
2610
2611 void
2612 zvol_remove_minors(spa_t *spa, const char *name, boolean_t async)
2613 {
2614 zvol_task_t *task;
2615 taskqid_t id;
2616
2617 task = zvol_task_alloc(ZVOL_ASYNC_REMOVE_MINORS, name, NULL, ~0ULL);
2618 if (task == NULL)
2619 return;
2620
2621 id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP);
2622 if ((async == B_FALSE) && (id != TASKQID_INVALID))
2623 taskq_wait_id(spa->spa_zvol_taskq, id);
2624 }
2625
2626 void
2627 zvol_rename_minors(spa_t *spa, const char *name1, const char *name2,
2628 boolean_t async)
2629 {
2630 zvol_task_t *task;
2631 taskqid_t id;
2632
2633 task = zvol_task_alloc(ZVOL_ASYNC_RENAME_MINORS, name1, name2, ~0ULL);
2634 if (task == NULL)
2635 return;
2636
2637 id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP);
2638 if ((async == B_FALSE) && (id != TASKQID_INVALID))
2639 taskq_wait_id(spa->spa_zvol_taskq, id);
2640 }
2641
2642 int
2643 zvol_init(void)
2644 {
2645 int threads = MIN(MAX(zvol_threads, 1), 1024);
2646 int i, error;
2647
2648 list_create(&zvol_state_list, sizeof (zvol_state_t),
2649 offsetof(zvol_state_t, zv_next));
2650 mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
2651 ida_init(&zvol_ida);
2652
2653 zvol_taskq = taskq_create(ZVOL_DRIVER, threads, maxclsyspri,
2654 threads * 2, INT_MAX, TASKQ_PREPOPULATE | TASKQ_DYNAMIC);
2655 if (zvol_taskq == NULL) {
2656 printk(KERN_INFO "ZFS: taskq_create() failed\n");
2657 error = -ENOMEM;
2658 goto out;
2659 }
2660
2661 zvol_htable = kmem_alloc(ZVOL_HT_SIZE * sizeof (struct hlist_head),
2662 KM_SLEEP);
2663 if (!zvol_htable) {
2664 error = -ENOMEM;
2665 goto out_taskq;
2666 }
2667 for (i = 0; i < ZVOL_HT_SIZE; i++)
2668 INIT_HLIST_HEAD(&zvol_htable[i]);
2669
2670 error = register_blkdev(zvol_major, ZVOL_DRIVER);
2671 if (error) {
2672 printk(KERN_INFO "ZFS: register_blkdev() failed %d\n", error);
2673 goto out_free;
2674 }
2675
2676 blk_register_region(MKDEV(zvol_major, 0), 1UL << MINORBITS,
2677 THIS_MODULE, zvol_probe, NULL, NULL);
2678
2679 return (0);
2680
2681 out_free:
2682 kmem_free(zvol_htable, ZVOL_HT_SIZE * sizeof (struct hlist_head));
2683 out_taskq:
2684 taskq_destroy(zvol_taskq);
2685 out:
2686 ida_destroy(&zvol_ida);
2687 mutex_destroy(&zvol_state_lock);
2688 list_destroy(&zvol_state_list);
2689
2690 return (SET_ERROR(error));
2691 }
2692
2693 void
2694 zvol_fini(void)
2695 {
2696 zvol_remove_minors_impl(NULL);
2697
2698 blk_unregister_region(MKDEV(zvol_major, 0), 1UL << MINORBITS);
2699 unregister_blkdev(zvol_major, ZVOL_DRIVER);
2700 kmem_free(zvol_htable, ZVOL_HT_SIZE * sizeof (struct hlist_head));
2701
2702 taskq_destroy(zvol_taskq);
2703 list_destroy(&zvol_state_list);
2704 mutex_destroy(&zvol_state_lock);
2705
2706 ida_destroy(&zvol_ida);
2707 }
2708
2709 /* BEGIN CSTYLED */
2710 module_param(zvol_inhibit_dev, uint, 0644);
2711 MODULE_PARM_DESC(zvol_inhibit_dev, "Do not create zvol device nodes");
2712
2713 module_param(zvol_major, uint, 0444);
2714 MODULE_PARM_DESC(zvol_major, "Major number for zvol device");
2715
2716 module_param(zvol_threads, uint, 0444);
2717 MODULE_PARM_DESC(zvol_threads, "Max number of threads to handle I/O requests");
2718
2719 module_param(zvol_request_sync, uint, 0644);
2720 MODULE_PARM_DESC(zvol_request_sync, "Synchronously handle bio requests");
2721
2722 module_param(zvol_max_discard_blocks, ulong, 0444);
2723 MODULE_PARM_DESC(zvol_max_discard_blocks, "Max number of blocks to discard");
2724
2725 module_param(zvol_prefetch_bytes, uint, 0644);
2726 MODULE_PARM_DESC(zvol_prefetch_bytes, "Prefetch N bytes at zvol start+end");
2727
2728 module_param(zvol_volmode, uint, 0644);
2729 MODULE_PARM_DESC(zvol_volmode, "Default volmode property value");
2730 /* END CSTYLED */