]> git.proxmox.com Git - mirror_zfs-debian.git/blob - module/zfs/zvol.c
zvol_probe should return NULL when the device isn't found.
[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
38 #include <sys/dmu_traverse.h>
39 #include <sys/dsl_dataset.h>
40 #include <sys/dsl_prop.h>
41 #include <sys/zap.h>
42 #include <sys/zil_impl.h>
43 #include <sys/zio.h>
44 #include <sys/zfs_rlock.h>
45 #include <sys/zfs_znode.h>
46 #include <sys/zvol.h>
47 #include <linux/blkdev_compat.h>
48
49 unsigned int zvol_inhibit_dev = 0;
50 unsigned int zvol_major = ZVOL_MAJOR;
51 unsigned int zvol_threads = 32;
52 unsigned long zvol_max_discard_blocks = 16384;
53
54 static taskq_t *zvol_taskq;
55 static kmutex_t zvol_state_lock;
56 static list_t zvol_state_list;
57 static char *zvol_tag = "zvol_tag";
58
59 /*
60 * The in-core state of each volume.
61 */
62 typedef struct zvol_state {
63 char zv_name[MAXNAMELEN]; /* name */
64 uint64_t zv_volsize; /* advertised space */
65 uint64_t zv_volblocksize;/* volume block size */
66 objset_t *zv_objset; /* objset handle */
67 uint32_t zv_flags; /* ZVOL_* flags */
68 uint32_t zv_open_count; /* open counts */
69 uint32_t zv_changed; /* disk changed */
70 zilog_t *zv_zilog; /* ZIL handle */
71 znode_t zv_znode; /* for range locking */
72 dmu_buf_t *zv_dbuf; /* bonus handle */
73 dev_t zv_dev; /* device id */
74 struct gendisk *zv_disk; /* generic disk */
75 struct request_queue *zv_queue; /* request queue */
76 spinlock_t zv_lock; /* request queue lock */
77 list_node_t zv_next; /* next zvol_state_t linkage */
78 } zvol_state_t;
79
80 #define ZVOL_RDONLY 0x1
81
82 /*
83 * Find the next available range of ZVOL_MINORS minor numbers. The
84 * zvol_state_list is kept in ascending minor order so we simply need
85 * to scan the list for the first gap in the sequence. This allows us
86 * to recycle minor number as devices are created and removed.
87 */
88 static int
89 zvol_find_minor(unsigned *minor)
90 {
91 zvol_state_t *zv;
92
93 *minor = 0;
94 ASSERT(MUTEX_HELD(&zvol_state_lock));
95 for (zv = list_head(&zvol_state_list); zv != NULL;
96 zv = list_next(&zvol_state_list, zv), *minor += ZVOL_MINORS) {
97 if (MINOR(zv->zv_dev) != MINOR(*minor))
98 break;
99 }
100
101 /* All minors are in use */
102 if (*minor >= (1 << MINORBITS))
103 return ENXIO;
104
105 return 0;
106 }
107
108 /*
109 * Find a zvol_state_t given the full major+minor dev_t.
110 */
111 static zvol_state_t *
112 zvol_find_by_dev(dev_t dev)
113 {
114 zvol_state_t *zv;
115
116 ASSERT(MUTEX_HELD(&zvol_state_lock));
117 for (zv = list_head(&zvol_state_list); zv != NULL;
118 zv = list_next(&zvol_state_list, zv)) {
119 if (zv->zv_dev == dev)
120 return zv;
121 }
122
123 return NULL;
124 }
125
126 /*
127 * Find a zvol_state_t given the name provided at zvol_alloc() time.
128 */
129 static zvol_state_t *
130 zvol_find_by_name(const char *name)
131 {
132 zvol_state_t *zv;
133
134 ASSERT(MUTEX_HELD(&zvol_state_lock));
135 for (zv = list_head(&zvol_state_list); zv != NULL;
136 zv = list_next(&zvol_state_list, zv)) {
137 if (!strncmp(zv->zv_name, name, MAXNAMELEN))
138 return zv;
139 }
140
141 return NULL;
142 }
143
144 /*
145 * ZFS_IOC_CREATE callback handles dmu zvol and zap object creation.
146 */
147 void
148 zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
149 {
150 zfs_creat_t *zct = arg;
151 nvlist_t *nvprops = zct->zct_props;
152 int error;
153 uint64_t volblocksize, volsize;
154
155 VERIFY(nvlist_lookup_uint64(nvprops,
156 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
157 if (nvlist_lookup_uint64(nvprops,
158 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
159 volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
160
161 /*
162 * These properties must be removed from the list so the generic
163 * property setting step won't apply to them.
164 */
165 VERIFY(nvlist_remove_all(nvprops,
166 zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
167 (void) nvlist_remove_all(nvprops,
168 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
169
170 error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
171 DMU_OT_NONE, 0, tx);
172 ASSERT(error == 0);
173
174 error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
175 DMU_OT_NONE, 0, tx);
176 ASSERT(error == 0);
177
178 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
179 ASSERT(error == 0);
180 }
181
182 /*
183 * ZFS_IOC_OBJSET_STATS entry point.
184 */
185 int
186 zvol_get_stats(objset_t *os, nvlist_t *nv)
187 {
188 int error;
189 dmu_object_info_t *doi;
190 uint64_t val;
191
192 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
193 if (error)
194 return (error);
195
196 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
197 doi = kmem_alloc(sizeof(dmu_object_info_t), KM_SLEEP);
198 error = dmu_object_info(os, ZVOL_OBJ, doi);
199
200 if (error == 0) {
201 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
202 doi->doi_data_block_size);
203 }
204
205 kmem_free(doi, sizeof(dmu_object_info_t));
206
207 return (error);
208 }
209
210 /*
211 * Sanity check volume size.
212 */
213 int
214 zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
215 {
216 if (volsize == 0)
217 return (EINVAL);
218
219 if (volsize % blocksize != 0)
220 return (EINVAL);
221
222 #ifdef _ILP32
223 if (volsize - 1 > MAXOFFSET_T)
224 return (EOVERFLOW);
225 #endif
226 return (0);
227 }
228
229 /*
230 * Ensure the zap is flushed then inform the VFS of the capacity change.
231 */
232 static int
233 zvol_update_volsize(zvol_state_t *zv, uint64_t volsize, objset_t *os)
234 {
235 struct block_device *bdev;
236 dmu_tx_t *tx;
237 int error;
238
239 ASSERT(MUTEX_HELD(&zvol_state_lock));
240
241 tx = dmu_tx_create(os);
242 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
243 error = dmu_tx_assign(tx, TXG_WAIT);
244 if (error) {
245 dmu_tx_abort(tx);
246 return (error);
247 }
248
249 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
250 &volsize, tx);
251 dmu_tx_commit(tx);
252
253 if (error)
254 return (error);
255
256 error = dmu_free_long_range(os,
257 ZVOL_OBJ, volsize, DMU_OBJECT_END);
258 if (error)
259 return (error);
260
261 bdev = bdget_disk(zv->zv_disk, 0);
262 if (!bdev)
263 return (EIO);
264 /*
265 * 2.6.28 API change
266 * Added check_disk_size_change() helper function.
267 */
268 #ifdef HAVE_CHECK_DISK_SIZE_CHANGE
269 set_capacity(zv->zv_disk, volsize >> 9);
270 zv->zv_volsize = volsize;
271 check_disk_size_change(zv->zv_disk, bdev);
272 #else
273 zv->zv_volsize = volsize;
274 zv->zv_changed = 1;
275 (void) check_disk_change(bdev);
276 #endif /* HAVE_CHECK_DISK_SIZE_CHANGE */
277
278 bdput(bdev);
279
280 return (0);
281 }
282
283 /*
284 * Set ZFS_PROP_VOLSIZE set entry point.
285 */
286 int
287 zvol_set_volsize(const char *name, uint64_t volsize)
288 {
289 zvol_state_t *zv;
290 dmu_object_info_t *doi;
291 objset_t *os = NULL;
292 uint64_t readonly;
293 int error;
294
295 mutex_enter(&zvol_state_lock);
296
297 zv = zvol_find_by_name(name);
298 if (zv == NULL) {
299 error = ENXIO;
300 goto out;
301 }
302
303 doi = kmem_alloc(sizeof(dmu_object_info_t), KM_SLEEP);
304
305 error = dmu_objset_hold(name, FTAG, &os);
306 if (error)
307 goto out_doi;
308
309 if ((error = dmu_object_info(os, ZVOL_OBJ, doi)) != 0 ||
310 (error = zvol_check_volsize(volsize,doi->doi_data_block_size)) != 0)
311 goto out_doi;
312
313 VERIFY(dsl_prop_get_integer(name, "readonly", &readonly, NULL) == 0);
314 if (readonly) {
315 error = EROFS;
316 goto out_doi;
317 }
318
319 if (get_disk_ro(zv->zv_disk) || (zv->zv_flags & ZVOL_RDONLY)) {
320 error = EROFS;
321 goto out_doi;
322 }
323
324 error = zvol_update_volsize(zv, volsize, os);
325 out_doi:
326 kmem_free(doi, sizeof(dmu_object_info_t));
327 out:
328 if (os)
329 dmu_objset_rele(os, FTAG);
330
331 mutex_exit(&zvol_state_lock);
332
333 return (error);
334 }
335
336 /*
337 * Sanity check volume block size.
338 */
339 int
340 zvol_check_volblocksize(uint64_t volblocksize)
341 {
342 if (volblocksize < SPA_MINBLOCKSIZE ||
343 volblocksize > SPA_MAXBLOCKSIZE ||
344 !ISP2(volblocksize))
345 return (EDOM);
346
347 return (0);
348 }
349
350 /*
351 * Set ZFS_PROP_VOLBLOCKSIZE set entry point.
352 */
353 int
354 zvol_set_volblocksize(const char *name, uint64_t volblocksize)
355 {
356 zvol_state_t *zv;
357 dmu_tx_t *tx;
358 int error;
359
360 mutex_enter(&zvol_state_lock);
361
362 zv = zvol_find_by_name(name);
363 if (zv == NULL) {
364 error = ENXIO;
365 goto out;
366 }
367
368 if (get_disk_ro(zv->zv_disk) || (zv->zv_flags & ZVOL_RDONLY)) {
369 error = EROFS;
370 goto out;
371 }
372
373 tx = dmu_tx_create(zv->zv_objset);
374 dmu_tx_hold_bonus(tx, ZVOL_OBJ);
375 error = dmu_tx_assign(tx, TXG_WAIT);
376 if (error) {
377 dmu_tx_abort(tx);
378 } else {
379 error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ,
380 volblocksize, 0, tx);
381 if (error == ENOTSUP)
382 error = EBUSY;
383 dmu_tx_commit(tx);
384 if (error == 0)
385 zv->zv_volblocksize = volblocksize;
386 }
387 out:
388 mutex_exit(&zvol_state_lock);
389
390 return (error);
391 }
392
393 /*
394 * Replay a TX_WRITE ZIL transaction that didn't get committed
395 * after a system failure
396 */
397 static int
398 zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
399 {
400 objset_t *os = zv->zv_objset;
401 char *data = (char *)(lr + 1); /* data follows lr_write_t */
402 uint64_t off = lr->lr_offset;
403 uint64_t len = lr->lr_length;
404 dmu_tx_t *tx;
405 int error;
406
407 if (byteswap)
408 byteswap_uint64_array(lr, sizeof (*lr));
409
410 tx = dmu_tx_create(os);
411 dmu_tx_hold_write(tx, ZVOL_OBJ, off, len);
412 error = dmu_tx_assign(tx, TXG_WAIT);
413 if (error) {
414 dmu_tx_abort(tx);
415 } else {
416 dmu_write(os, ZVOL_OBJ, off, len, data, tx);
417 dmu_tx_commit(tx);
418 }
419
420 return (error);
421 }
422
423 static int
424 zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
425 {
426 return (ENOTSUP);
427 }
428
429 /*
430 * Callback vectors for replaying records.
431 * Only TX_WRITE is needed for zvol.
432 */
433 zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = {
434 (zil_replay_func_t *)zvol_replay_err, /* no such transaction type */
435 (zil_replay_func_t *)zvol_replay_err, /* TX_CREATE */
436 (zil_replay_func_t *)zvol_replay_err, /* TX_MKDIR */
437 (zil_replay_func_t *)zvol_replay_err, /* TX_MKXATTR */
438 (zil_replay_func_t *)zvol_replay_err, /* TX_SYMLINK */
439 (zil_replay_func_t *)zvol_replay_err, /* TX_REMOVE */
440 (zil_replay_func_t *)zvol_replay_err, /* TX_RMDIR */
441 (zil_replay_func_t *)zvol_replay_err, /* TX_LINK */
442 (zil_replay_func_t *)zvol_replay_err, /* TX_RENAME */
443 (zil_replay_func_t *)zvol_replay_write, /* TX_WRITE */
444 (zil_replay_func_t *)zvol_replay_err, /* TX_TRUNCATE */
445 (zil_replay_func_t *)zvol_replay_err, /* TX_SETATTR */
446 (zil_replay_func_t *)zvol_replay_err, /* TX_ACL */
447 };
448
449 /*
450 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
451 *
452 * We store data in the log buffers if it's small enough.
453 * Otherwise we will later flush the data out via dmu_sync().
454 */
455 ssize_t zvol_immediate_write_sz = 32768;
456
457 static void
458 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx,
459 uint64_t offset, uint64_t size, int sync)
460 {
461 uint32_t blocksize = zv->zv_volblocksize;
462 zilog_t *zilog = zv->zv_zilog;
463 boolean_t slogging;
464 ssize_t immediate_write_sz;
465
466 if (zil_replaying(zilog, tx))
467 return;
468
469 immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
470 ? 0 : zvol_immediate_write_sz;
471 slogging = spa_has_slogs(zilog->zl_spa) &&
472 (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
473
474 while (size) {
475 itx_t *itx;
476 lr_write_t *lr;
477 ssize_t len;
478 itx_wr_state_t write_state;
479
480 /*
481 * Unlike zfs_log_write() we can be called with
482 * up to DMU_MAX_ACCESS/2 (5MB) writes.
483 */
484 if (blocksize > immediate_write_sz && !slogging &&
485 size >= blocksize && offset % blocksize == 0) {
486 write_state = WR_INDIRECT; /* uses dmu_sync */
487 len = blocksize;
488 } else if (sync) {
489 write_state = WR_COPIED;
490 len = MIN(ZIL_MAX_LOG_DATA, size);
491 } else {
492 write_state = WR_NEED_COPY;
493 len = MIN(ZIL_MAX_LOG_DATA, size);
494 }
495
496 itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
497 (write_state == WR_COPIED ? len : 0));
498 lr = (lr_write_t *)&itx->itx_lr;
499 if (write_state == WR_COPIED && dmu_read(zv->zv_objset,
500 ZVOL_OBJ, offset, len, lr+1, DMU_READ_NO_PREFETCH) != 0) {
501 zil_itx_destroy(itx);
502 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
503 lr = (lr_write_t *)&itx->itx_lr;
504 write_state = WR_NEED_COPY;
505 }
506
507 itx->itx_wr_state = write_state;
508 if (write_state == WR_NEED_COPY)
509 itx->itx_sod += len;
510 lr->lr_foid = ZVOL_OBJ;
511 lr->lr_offset = offset;
512 lr->lr_length = len;
513 lr->lr_blkoff = 0;
514 BP_ZERO(&lr->lr_blkptr);
515
516 itx->itx_private = zv;
517 itx->itx_sync = sync;
518
519 (void) zil_itx_assign(zilog, itx, tx);
520
521 offset += len;
522 size -= len;
523 }
524 }
525
526 /*
527 * Common write path running under the zvol taskq context. This function
528 * is responsible for copying the request structure data in to the DMU and
529 * signaling the request queue with the result of the copy.
530 */
531 static void
532 zvol_write(void *arg)
533 {
534 struct request *req = (struct request *)arg;
535 struct request_queue *q = req->q;
536 zvol_state_t *zv = q->queuedata;
537 uint64_t offset = blk_rq_pos(req) << 9;
538 uint64_t size = blk_rq_bytes(req);
539 int error = 0;
540 dmu_tx_t *tx;
541 rl_t *rl;
542
543 /*
544 * Annotate this call path with a flag that indicates that it is
545 * unsafe to use KM_SLEEP during memory allocations due to the
546 * potential for a deadlock. KM_PUSHPAGE should be used instead.
547 */
548 ASSERT(!(current->flags & PF_NOFS));
549 current->flags |= PF_NOFS;
550
551 if (req->cmd_flags & VDEV_REQ_FLUSH)
552 zil_commit(zv->zv_zilog, ZVOL_OBJ);
553
554 /*
555 * Some requests are just for flush and nothing else.
556 */
557 if (size == 0) {
558 blk_end_request(req, 0, size);
559 goto out;
560 }
561
562 rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_WRITER);
563
564 tx = dmu_tx_create(zv->zv_objset);
565 dmu_tx_hold_write(tx, ZVOL_OBJ, offset, size);
566
567 /* This will only fail for ENOSPC */
568 error = dmu_tx_assign(tx, TXG_WAIT);
569 if (error) {
570 dmu_tx_abort(tx);
571 zfs_range_unlock(rl);
572 blk_end_request(req, -error, size);
573 goto out;
574 }
575
576 error = dmu_write_req(zv->zv_objset, ZVOL_OBJ, req, tx);
577 if (error == 0)
578 zvol_log_write(zv, tx, offset, size,
579 req->cmd_flags & VDEV_REQ_FUA);
580
581 dmu_tx_commit(tx);
582 zfs_range_unlock(rl);
583
584 if ((req->cmd_flags & VDEV_REQ_FUA) ||
585 zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)
586 zil_commit(zv->zv_zilog, ZVOL_OBJ);
587
588 blk_end_request(req, -error, size);
589 out:
590 current->flags &= ~PF_NOFS;
591 }
592
593 #ifdef HAVE_BLK_QUEUE_DISCARD
594 static void
595 zvol_discard(void *arg)
596 {
597 struct request *req = (struct request *)arg;
598 struct request_queue *q = req->q;
599 zvol_state_t *zv = q->queuedata;
600 uint64_t offset = blk_rq_pos(req) << 9;
601 uint64_t size = blk_rq_bytes(req);
602 int error;
603 rl_t *rl;
604
605 /*
606 * Annotate this call path with a flag that indicates that it is
607 * unsafe to use KM_SLEEP during memory allocations due to the
608 * potential for a deadlock. KM_PUSHPAGE should be used instead.
609 */
610 ASSERT(!(current->flags & PF_NOFS));
611 current->flags |= PF_NOFS;
612
613 if (offset + size > zv->zv_volsize) {
614 blk_end_request(req, -EIO, size);
615 goto out;
616 }
617
618 if (size == 0) {
619 blk_end_request(req, 0, size);
620 goto out;
621 }
622
623 rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_WRITER);
624
625 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, offset, size);
626
627 /*
628 * TODO: maybe we should add the operation to the log.
629 */
630
631 zfs_range_unlock(rl);
632
633 blk_end_request(req, -error, size);
634 out:
635 current->flags &= ~PF_NOFS;
636 }
637 #endif /* HAVE_BLK_QUEUE_DISCARD */
638
639 /*
640 * Common read path running under the zvol taskq context. This function
641 * is responsible for copying the requested data out of the DMU and in to
642 * a linux request structure. It then must signal the request queue with
643 * an error code describing the result of the copy.
644 */
645 static void
646 zvol_read(void *arg)
647 {
648 struct request *req = (struct request *)arg;
649 struct request_queue *q = req->q;
650 zvol_state_t *zv = q->queuedata;
651 uint64_t offset = blk_rq_pos(req) << 9;
652 uint64_t size = blk_rq_bytes(req);
653 int error;
654 rl_t *rl;
655
656 if (size == 0) {
657 blk_end_request(req, 0, size);
658 return;
659 }
660
661 rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
662
663 error = dmu_read_req(zv->zv_objset, ZVOL_OBJ, req);
664
665 zfs_range_unlock(rl);
666
667 /* convert checksum errors into IO errors */
668 if (error == ECKSUM)
669 error = EIO;
670
671 blk_end_request(req, -error, size);
672 }
673
674 /*
675 * Request will be added back to the request queue and retried if
676 * it cannot be immediately dispatched to the taskq for handling
677 */
678 static inline void
679 zvol_dispatch(task_func_t func, struct request *req)
680 {
681 if (!taskq_dispatch(zvol_taskq, func, (void *)req, TQ_NOSLEEP))
682 blk_requeue_request(req->q, req);
683 }
684
685 /*
686 * Common request path. Rather than registering a custom make_request()
687 * function we use the generic Linux version. This is done because it allows
688 * us to easily merge read requests which would otherwise we performed
689 * synchronously by the DMU. This is less critical in write case where the
690 * DMU will perform the correct merging within a transaction group. Using
691 * the generic make_request() also let's use leverage the fact that the
692 * elevator with ensure correct ordering in regards to barrior IOs. On
693 * the downside it means that in the write case we end up doing request
694 * merging twice once in the elevator and once in the DMU.
695 *
696 * The request handler is called under a spin lock so all the real work
697 * is handed off to be done in the context of the zvol taskq. This function
698 * simply performs basic request sanity checking and hands off the request.
699 */
700 static void
701 zvol_request(struct request_queue *q)
702 {
703 zvol_state_t *zv = q->queuedata;
704 struct request *req;
705 unsigned int size;
706
707 while ((req = blk_fetch_request(q)) != NULL) {
708 size = blk_rq_bytes(req);
709
710 if (size != 0 && blk_rq_pos(req) + blk_rq_sectors(req) >
711 get_capacity(zv->zv_disk)) {
712 printk(KERN_INFO
713 "%s: bad access: block=%llu, count=%lu\n",
714 req->rq_disk->disk_name,
715 (long long unsigned)blk_rq_pos(req),
716 (long unsigned)blk_rq_sectors(req));
717 __blk_end_request(req, -EIO, size);
718 continue;
719 }
720
721 if (!blk_fs_request(req)) {
722 printk(KERN_INFO "%s: non-fs cmd\n",
723 req->rq_disk->disk_name);
724 __blk_end_request(req, -EIO, size);
725 continue;
726 }
727
728 switch (rq_data_dir(req)) {
729 case READ:
730 zvol_dispatch(zvol_read, req);
731 break;
732 case WRITE:
733 if (unlikely(get_disk_ro(zv->zv_disk)) ||
734 unlikely(zv->zv_flags & ZVOL_RDONLY)) {
735 __blk_end_request(req, -EROFS, size);
736 break;
737 }
738
739 #ifdef HAVE_BLK_QUEUE_DISCARD
740 if (req->cmd_flags & VDEV_REQ_DISCARD) {
741 zvol_dispatch(zvol_discard, req);
742 break;
743 }
744 #endif /* HAVE_BLK_QUEUE_DISCARD */
745
746 zvol_dispatch(zvol_write, req);
747 break;
748 default:
749 printk(KERN_INFO "%s: unknown cmd: %d\n",
750 req->rq_disk->disk_name, (int)rq_data_dir(req));
751 __blk_end_request(req, -EIO, size);
752 break;
753 }
754 }
755 }
756
757 static void
758 zvol_get_done(zgd_t *zgd, int error)
759 {
760 if (zgd->zgd_db)
761 dmu_buf_rele(zgd->zgd_db, zgd);
762
763 zfs_range_unlock(zgd->zgd_rl);
764
765 if (error == 0 && zgd->zgd_bp)
766 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
767
768 kmem_free(zgd, sizeof (zgd_t));
769 }
770
771 /*
772 * Get data to generate a TX_WRITE intent log record.
773 */
774 static int
775 zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
776 {
777 zvol_state_t *zv = arg;
778 objset_t *os = zv->zv_objset;
779 uint64_t offset = lr->lr_offset;
780 uint64_t size = lr->lr_length;
781 dmu_buf_t *db;
782 zgd_t *zgd;
783 int error;
784
785 ASSERT(zio != NULL);
786 ASSERT(size != 0);
787
788 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_PUSHPAGE);
789 zgd->zgd_zilog = zv->zv_zilog;
790 zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
791
792 /*
793 * Write records come in two flavors: immediate and indirect.
794 * For small writes it's cheaper to store the data with the
795 * log record (immediate); for large writes it's cheaper to
796 * sync the data and get a pointer to it (indirect) so that
797 * we don't have to write the data twice.
798 */
799 if (buf != NULL) { /* immediate write */
800 error = dmu_read(os, ZVOL_OBJ, offset, size, buf,
801 DMU_READ_NO_PREFETCH);
802 } else {
803 size = zv->zv_volblocksize;
804 offset = P2ALIGN_TYPED(offset, size, uint64_t);
805 error = dmu_buf_hold(os, ZVOL_OBJ, offset, zgd, &db,
806 DMU_READ_NO_PREFETCH);
807 if (error == 0) {
808 zgd->zgd_db = db;
809 zgd->zgd_bp = &lr->lr_blkptr;
810
811 ASSERT(db != NULL);
812 ASSERT(db->db_offset == offset);
813 ASSERT(db->db_size == size);
814
815 error = dmu_sync(zio, lr->lr_common.lrc_txg,
816 zvol_get_done, zgd);
817
818 if (error == 0)
819 return (0);
820 }
821 }
822
823 zvol_get_done(zgd, error);
824
825 return (error);
826 }
827
828 /*
829 * The zvol_state_t's are inserted in increasing MINOR(dev_t) order.
830 */
831 static void
832 zvol_insert(zvol_state_t *zv_insert)
833 {
834 zvol_state_t *zv = NULL;
835
836 ASSERT(MUTEX_HELD(&zvol_state_lock));
837 ASSERT3U(MINOR(zv_insert->zv_dev) & ZVOL_MINOR_MASK, ==, 0);
838 for (zv = list_head(&zvol_state_list); zv != NULL;
839 zv = list_next(&zvol_state_list, zv)) {
840 if (MINOR(zv->zv_dev) > MINOR(zv_insert->zv_dev))
841 break;
842 }
843
844 list_insert_before(&zvol_state_list, zv, zv_insert);
845 }
846
847 /*
848 * Simply remove the zvol from to list of zvols.
849 */
850 static void
851 zvol_remove(zvol_state_t *zv_remove)
852 {
853 ASSERT(MUTEX_HELD(&zvol_state_lock));
854 list_remove(&zvol_state_list, zv_remove);
855 }
856
857 static int
858 zvol_first_open(zvol_state_t *zv)
859 {
860 objset_t *os;
861 uint64_t volsize;
862 int error;
863 uint64_t ro;
864
865 /* lie and say we're read-only */
866 error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, 1, zvol_tag, &os);
867 if (error)
868 return (-error);
869
870 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
871 if (error) {
872 dmu_objset_disown(os, zvol_tag);
873 return (-error);
874 }
875
876 zv->zv_objset = os;
877 error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf);
878 if (error) {
879 dmu_objset_disown(os, zvol_tag);
880 return (-error);
881 }
882
883 set_capacity(zv->zv_disk, volsize >> 9);
884 zv->zv_volsize = volsize;
885 zv->zv_zilog = zil_open(os, zvol_get_data);
886
887 VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &ro, NULL) == 0);
888 if (ro || dmu_objset_is_snapshot(os)) {
889 set_disk_ro(zv->zv_disk, 1);
890 zv->zv_flags |= ZVOL_RDONLY;
891 } else {
892 set_disk_ro(zv->zv_disk, 0);
893 zv->zv_flags &= ~ZVOL_RDONLY;
894 }
895
896 return (-error);
897 }
898
899 static void
900 zvol_last_close(zvol_state_t *zv)
901 {
902 zil_close(zv->zv_zilog);
903 zv->zv_zilog = NULL;
904 dmu_buf_rele(zv->zv_dbuf, zvol_tag);
905 zv->zv_dbuf = NULL;
906 dmu_objset_disown(zv->zv_objset, zvol_tag);
907 zv->zv_objset = NULL;
908 }
909
910 static int
911 zvol_open(struct block_device *bdev, fmode_t flag)
912 {
913 zvol_state_t *zv = bdev->bd_disk->private_data;
914 int error = 0, drop_mutex = 0;
915
916 /*
917 * If the caller is already holding the mutex do not take it
918 * again, this will happen as part of zvol_create_minor().
919 * Once add_disk() is called the device is live and the kernel
920 * will attempt to open it to read the partition information.
921 */
922 if (!mutex_owned(&zvol_state_lock)) {
923 mutex_enter(&zvol_state_lock);
924 drop_mutex = 1;
925 }
926
927 ASSERT3P(zv, !=, NULL);
928
929 if (zv->zv_open_count == 0) {
930 error = zvol_first_open(zv);
931 if (error)
932 goto out_mutex;
933 }
934
935 if ((flag & FMODE_WRITE) &&
936 (get_disk_ro(zv->zv_disk) || (zv->zv_flags & ZVOL_RDONLY))) {
937 error = -EROFS;
938 goto out_open_count;
939 }
940
941 zv->zv_open_count++;
942
943 out_open_count:
944 if (zv->zv_open_count == 0)
945 zvol_last_close(zv);
946
947 out_mutex:
948 if (drop_mutex)
949 mutex_exit(&zvol_state_lock);
950
951 check_disk_change(bdev);
952
953 return (error);
954 }
955
956 static int
957 zvol_release(struct gendisk *disk, fmode_t mode)
958 {
959 zvol_state_t *zv = disk->private_data;
960 int drop_mutex = 0;
961
962 if (!mutex_owned(&zvol_state_lock)) {
963 mutex_enter(&zvol_state_lock);
964 drop_mutex = 1;
965 }
966
967 ASSERT3P(zv, !=, NULL);
968 ASSERT3U(zv->zv_open_count, >, 0);
969 zv->zv_open_count--;
970 if (zv->zv_open_count == 0)
971 zvol_last_close(zv);
972
973 if (drop_mutex)
974 mutex_exit(&zvol_state_lock);
975
976 return (0);
977 }
978
979 static int
980 zvol_ioctl(struct block_device *bdev, fmode_t mode,
981 unsigned int cmd, unsigned long arg)
982 {
983 zvol_state_t *zv = bdev->bd_disk->private_data;
984 int error = 0;
985
986 if (zv == NULL)
987 return (-ENXIO);
988
989 switch (cmd) {
990 case BLKFLSBUF:
991 zil_commit(zv->zv_zilog, ZVOL_OBJ);
992 break;
993 case BLKZNAME:
994 error = copy_to_user((void *)arg, zv->zv_name, MAXNAMELEN);
995 break;
996
997 default:
998 error = -ENOTTY;
999 break;
1000
1001 }
1002
1003 return (error);
1004 }
1005
1006 #ifdef CONFIG_COMPAT
1007 static int
1008 zvol_compat_ioctl(struct block_device *bdev, fmode_t mode,
1009 unsigned cmd, unsigned long arg)
1010 {
1011 return zvol_ioctl(bdev, mode, cmd, arg);
1012 }
1013 #else
1014 #define zvol_compat_ioctl NULL
1015 #endif
1016
1017 static int zvol_media_changed(struct gendisk *disk)
1018 {
1019 zvol_state_t *zv = disk->private_data;
1020
1021 return zv->zv_changed;
1022 }
1023
1024 static int zvol_revalidate_disk(struct gendisk *disk)
1025 {
1026 zvol_state_t *zv = disk->private_data;
1027
1028 zv->zv_changed = 0;
1029 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1030
1031 return 0;
1032 }
1033
1034 /*
1035 * Provide a simple virtual geometry for legacy compatibility. For devices
1036 * smaller than 1 MiB a small head and sector count is used to allow very
1037 * tiny devices. For devices over 1 Mib a standard head and sector count
1038 * is used to keep the cylinders count reasonable.
1039 */
1040 static int
1041 zvol_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1042 {
1043 zvol_state_t *zv = bdev->bd_disk->private_data;
1044 sector_t sectors = get_capacity(zv->zv_disk);
1045
1046 if (sectors > 2048) {
1047 geo->heads = 16;
1048 geo->sectors = 63;
1049 } else {
1050 geo->heads = 2;
1051 geo->sectors = 4;
1052 }
1053
1054 geo->start = 0;
1055 geo->cylinders = sectors / (geo->heads * geo->sectors);
1056
1057 return 0;
1058 }
1059
1060 static struct kobject *
1061 zvol_probe(dev_t dev, int *part, void *arg)
1062 {
1063 zvol_state_t *zv;
1064 struct kobject *kobj;
1065
1066 mutex_enter(&zvol_state_lock);
1067 zv = zvol_find_by_dev(dev);
1068 kobj = zv ? get_disk(zv->zv_disk) : NULL;
1069 mutex_exit(&zvol_state_lock);
1070
1071 return kobj;
1072 }
1073
1074 #ifdef HAVE_BDEV_BLOCK_DEVICE_OPERATIONS
1075 static struct block_device_operations zvol_ops = {
1076 .open = zvol_open,
1077 .release = zvol_release,
1078 .ioctl = zvol_ioctl,
1079 .compat_ioctl = zvol_compat_ioctl,
1080 .media_changed = zvol_media_changed,
1081 .revalidate_disk = zvol_revalidate_disk,
1082 .getgeo = zvol_getgeo,
1083 .owner = THIS_MODULE,
1084 };
1085
1086 #else /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1087
1088 static int
1089 zvol_open_by_inode(struct inode *inode, struct file *file)
1090 {
1091 return zvol_open(inode->i_bdev, file->f_mode);
1092 }
1093
1094 static int
1095 zvol_release_by_inode(struct inode *inode, struct file *file)
1096 {
1097 return zvol_release(inode->i_bdev->bd_disk, file->f_mode);
1098 }
1099
1100 static int
1101 zvol_ioctl_by_inode(struct inode *inode, struct file *file,
1102 unsigned int cmd, unsigned long arg)
1103 {
1104 if (file == NULL || inode == NULL)
1105 return -EINVAL;
1106 return zvol_ioctl(inode->i_bdev, file->f_mode, cmd, arg);
1107 }
1108
1109 # ifdef CONFIG_COMPAT
1110 static long
1111 zvol_compat_ioctl_by_inode(struct file *file,
1112 unsigned int cmd, unsigned long arg)
1113 {
1114 if (file == NULL)
1115 return -EINVAL;
1116 return zvol_compat_ioctl(file->f_dentry->d_inode->i_bdev,
1117 file->f_mode, cmd, arg);
1118 }
1119 # else
1120 # define zvol_compat_ioctl_by_inode NULL
1121 # endif
1122
1123 static struct block_device_operations zvol_ops = {
1124 .open = zvol_open_by_inode,
1125 .release = zvol_release_by_inode,
1126 .ioctl = zvol_ioctl_by_inode,
1127 .compat_ioctl = zvol_compat_ioctl_by_inode,
1128 .media_changed = zvol_media_changed,
1129 .revalidate_disk = zvol_revalidate_disk,
1130 .getgeo = zvol_getgeo,
1131 .owner = THIS_MODULE,
1132 };
1133 #endif /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1134
1135 /*
1136 * Allocate memory for a new zvol_state_t and setup the required
1137 * request queue and generic disk structures for the block device.
1138 */
1139 static zvol_state_t *
1140 zvol_alloc(dev_t dev, const char *name)
1141 {
1142 zvol_state_t *zv;
1143
1144 zv = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
1145 if (zv == NULL)
1146 goto out;
1147
1148 zv->zv_queue = blk_init_queue(zvol_request, &zv->zv_lock);
1149 if (zv->zv_queue == NULL)
1150 goto out_kmem;
1151
1152 #ifdef HAVE_BLK_QUEUE_FLUSH
1153 blk_queue_flush(zv->zv_queue, VDEV_REQ_FLUSH | VDEV_REQ_FUA);
1154 #else
1155 blk_queue_ordered(zv->zv_queue, QUEUE_ORDERED_DRAIN, NULL);
1156 #endif /* HAVE_BLK_QUEUE_FLUSH */
1157
1158 zv->zv_disk = alloc_disk(ZVOL_MINORS);
1159 if (zv->zv_disk == NULL)
1160 goto out_queue;
1161
1162 zv->zv_queue->queuedata = zv;
1163 zv->zv_dev = dev;
1164 zv->zv_open_count = 0;
1165 strlcpy(zv->zv_name, name, MAXNAMELEN);
1166
1167 mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
1168 avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
1169 sizeof (rl_t), offsetof(rl_t, r_node));
1170 zv->zv_znode.z_is_zvol = TRUE;
1171
1172 spin_lock_init(&zv->zv_lock);
1173 list_link_init(&zv->zv_next);
1174
1175 zv->zv_disk->major = zvol_major;
1176 zv->zv_disk->first_minor = (dev & MINORMASK);
1177 zv->zv_disk->fops = &zvol_ops;
1178 zv->zv_disk->private_data = zv;
1179 zv->zv_disk->queue = zv->zv_queue;
1180 snprintf(zv->zv_disk->disk_name, DISK_NAME_LEN, "%s%d",
1181 ZVOL_DEV_NAME, (dev & MINORMASK));
1182
1183 return zv;
1184
1185 out_queue:
1186 blk_cleanup_queue(zv->zv_queue);
1187 out_kmem:
1188 kmem_free(zv, sizeof (zvol_state_t));
1189 out:
1190 return NULL;
1191 }
1192
1193 /*
1194 * Cleanup then free a zvol_state_t which was created by zvol_alloc().
1195 */
1196 static void
1197 zvol_free(zvol_state_t *zv)
1198 {
1199 avl_destroy(&zv->zv_znode.z_range_avl);
1200 mutex_destroy(&zv->zv_znode.z_range_lock);
1201
1202 del_gendisk(zv->zv_disk);
1203 blk_cleanup_queue(zv->zv_queue);
1204 put_disk(zv->zv_disk);
1205
1206 kmem_free(zv, sizeof (zvol_state_t));
1207 }
1208
1209 static int
1210 __zvol_create_minor(const char *name)
1211 {
1212 zvol_state_t *zv;
1213 objset_t *os;
1214 dmu_object_info_t *doi;
1215 uint64_t volsize;
1216 unsigned minor = 0;
1217 int error = 0;
1218
1219 ASSERT(MUTEX_HELD(&zvol_state_lock));
1220
1221 zv = zvol_find_by_name(name);
1222 if (zv) {
1223 error = EEXIST;
1224 goto out;
1225 }
1226
1227 doi = kmem_alloc(sizeof(dmu_object_info_t), KM_SLEEP);
1228
1229 error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os);
1230 if (error)
1231 goto out_doi;
1232
1233 error = dmu_object_info(os, ZVOL_OBJ, doi);
1234 if (error)
1235 goto out_dmu_objset_disown;
1236
1237 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
1238 if (error)
1239 goto out_dmu_objset_disown;
1240
1241 error = zvol_find_minor(&minor);
1242 if (error)
1243 goto out_dmu_objset_disown;
1244
1245 zv = zvol_alloc(MKDEV(zvol_major, minor), name);
1246 if (zv == NULL) {
1247 error = EAGAIN;
1248 goto out_dmu_objset_disown;
1249 }
1250
1251 if (dmu_objset_is_snapshot(os))
1252 zv->zv_flags |= ZVOL_RDONLY;
1253
1254 zv->zv_volblocksize = doi->doi_data_block_size;
1255 zv->zv_volsize = volsize;
1256 zv->zv_objset = os;
1257
1258 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1259
1260 blk_queue_max_hw_sectors(zv->zv_queue, UINT_MAX);
1261 blk_queue_max_segments(zv->zv_queue, UINT16_MAX);
1262 blk_queue_max_segment_size(zv->zv_queue, UINT_MAX);
1263 blk_queue_physical_block_size(zv->zv_queue, zv->zv_volblocksize);
1264 blk_queue_io_opt(zv->zv_queue, zv->zv_volblocksize);
1265 #ifdef HAVE_BLK_QUEUE_DISCARD
1266 blk_queue_max_discard_sectors(zv->zv_queue,
1267 (zvol_max_discard_blocks * zv->zv_volblocksize) >> 9);
1268 blk_queue_discard_granularity(zv->zv_queue, zv->zv_volblocksize);
1269 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, zv->zv_queue);
1270 #endif
1271 #ifdef HAVE_BLK_QUEUE_NONROT
1272 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zv->zv_queue);
1273 #endif
1274
1275 if (zil_replay_disable)
1276 zil_destroy(dmu_objset_zil(os), B_FALSE);
1277 else
1278 zil_replay(os, zv, zvol_replay_vector);
1279
1280 out_dmu_objset_disown:
1281 dmu_objset_disown(os, zvol_tag);
1282 zv->zv_objset = NULL;
1283 out_doi:
1284 kmem_free(doi, sizeof(dmu_object_info_t));
1285 out:
1286
1287 if (error == 0) {
1288 zvol_insert(zv);
1289 add_disk(zv->zv_disk);
1290 }
1291
1292 return (error);
1293 }
1294
1295 /*
1296 * Create a block device minor node and setup the linkage between it
1297 * and the specified volume. Once this function returns the block
1298 * device is live and ready for use.
1299 */
1300 int
1301 zvol_create_minor(const char *name)
1302 {
1303 int error;
1304
1305 mutex_enter(&zvol_state_lock);
1306 error = __zvol_create_minor(name);
1307 mutex_exit(&zvol_state_lock);
1308
1309 return (error);
1310 }
1311
1312 static int
1313 __zvol_remove_minor(const char *name)
1314 {
1315 zvol_state_t *zv;
1316
1317 ASSERT(MUTEX_HELD(&zvol_state_lock));
1318
1319 zv = zvol_find_by_name(name);
1320 if (zv == NULL)
1321 return (ENXIO);
1322
1323 if (zv->zv_open_count > 0)
1324 return (EBUSY);
1325
1326 zvol_remove(zv);
1327 zvol_free(zv);
1328
1329 return (0);
1330 }
1331
1332 /*
1333 * Remove a block device minor node for the specified volume.
1334 */
1335 int
1336 zvol_remove_minor(const char *name)
1337 {
1338 int error;
1339
1340 mutex_enter(&zvol_state_lock);
1341 error = __zvol_remove_minor(name);
1342 mutex_exit(&zvol_state_lock);
1343
1344 return (error);
1345 }
1346
1347 static int
1348 zvol_create_minors_cb(spa_t *spa, uint64_t dsobj,
1349 const char *dsname, void *arg)
1350 {
1351 if (strchr(dsname, '/') == NULL)
1352 return 0;
1353
1354 (void) __zvol_create_minor(dsname);
1355 return (0);
1356 }
1357
1358 /*
1359 * Create minors for specified pool, if pool is NULL create minors
1360 * for all available pools.
1361 */
1362 int
1363 zvol_create_minors(const char *pool)
1364 {
1365 spa_t *spa = NULL;
1366 int error = 0;
1367
1368 if (zvol_inhibit_dev)
1369 return (0);
1370
1371 mutex_enter(&zvol_state_lock);
1372 if (pool) {
1373 error = dmu_objset_find_spa(NULL, pool, zvol_create_minors_cb,
1374 NULL, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1375 } else {
1376 mutex_enter(&spa_namespace_lock);
1377 while ((spa = spa_next(spa)) != NULL) {
1378 error = dmu_objset_find_spa(NULL,
1379 spa_name(spa), zvol_create_minors_cb, NULL,
1380 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1381 if (error)
1382 break;
1383 }
1384 mutex_exit(&spa_namespace_lock);
1385 }
1386 mutex_exit(&zvol_state_lock);
1387
1388 return error;
1389 }
1390
1391 /*
1392 * Remove minors for specified pool, if pool is NULL remove all minors.
1393 */
1394 void
1395 zvol_remove_minors(const char *pool)
1396 {
1397 zvol_state_t *zv, *zv_next;
1398 char *str;
1399
1400 if (zvol_inhibit_dev)
1401 return;
1402
1403 str = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
1404 if (pool) {
1405 (void) strncpy(str, pool, strlen(pool));
1406 (void) strcat(str, "/");
1407 }
1408
1409 mutex_enter(&zvol_state_lock);
1410 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1411 zv_next = list_next(&zvol_state_list, zv);
1412
1413 if (pool == NULL || !strncmp(str, zv->zv_name, strlen(str))) {
1414 zvol_remove(zv);
1415 zvol_free(zv);
1416 }
1417 }
1418 mutex_exit(&zvol_state_lock);
1419 kmem_free(str, MAXNAMELEN);
1420 }
1421
1422 int
1423 zvol_init(void)
1424 {
1425 int error;
1426
1427 zvol_taskq = taskq_create(ZVOL_DRIVER, zvol_threads, maxclsyspri,
1428 zvol_threads, INT_MAX, TASKQ_PREPOPULATE);
1429 if (zvol_taskq == NULL) {
1430 printk(KERN_INFO "ZFS: taskq_create() failed\n");
1431 return (-ENOMEM);
1432 }
1433
1434 error = register_blkdev(zvol_major, ZVOL_DRIVER);
1435 if (error) {
1436 printk(KERN_INFO "ZFS: register_blkdev() failed %d\n", error);
1437 taskq_destroy(zvol_taskq);
1438 return (error);
1439 }
1440
1441 blk_register_region(MKDEV(zvol_major, 0), 1UL << MINORBITS,
1442 THIS_MODULE, zvol_probe, NULL, NULL);
1443
1444 mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
1445 list_create(&zvol_state_list, sizeof (zvol_state_t),
1446 offsetof(zvol_state_t, zv_next));
1447
1448 (void) zvol_create_minors(NULL);
1449
1450 return (0);
1451 }
1452
1453 void
1454 zvol_fini(void)
1455 {
1456 zvol_remove_minors(NULL);
1457 blk_unregister_region(MKDEV(zvol_major, 0), 1UL << MINORBITS);
1458 unregister_blkdev(zvol_major, ZVOL_DRIVER);
1459 taskq_destroy(zvol_taskq);
1460 mutex_destroy(&zvol_state_lock);
1461 list_destroy(&zvol_state_list);
1462 }
1463
1464 module_param(zvol_inhibit_dev, uint, 0644);
1465 MODULE_PARM_DESC(zvol_inhibit_dev, "Do not create zvol device nodes");
1466
1467 module_param(zvol_major, uint, 0444);
1468 MODULE_PARM_DESC(zvol_major, "Major number for zvol device");
1469
1470 module_param(zvol_threads, uint, 0444);
1471 MODULE_PARM_DESC(zvol_threads, "Number of threads for zvol device");
1472
1473 module_param(zvol_max_discard_blocks, ulong, 0444);
1474 MODULE_PARM_DESC(zvol_max_discard_blocks, "Max number of blocks to discard at once");