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