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