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