]> git.proxmox.com Git - mirror_zfs-debian.git/blame - module/zfs/zvol.c
Add missing dependencies to ./copy-builtin
[mirror_zfs-debian.git] / module / zfs / zvol.c
CommitLineData
60101509
BB
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>
61e90960 47#include <linux/blkdev_compat.h>
60101509 48
74497b7a 49unsigned int zvol_inhibit_dev = 0;
60101509 50unsigned int zvol_major = ZVOL_MAJOR;
dde9380a 51unsigned int zvol_threads = 32;
7c0e5708 52unsigned long zvol_max_discard_blocks = 16384;
60101509
BB
53
54static taskq_t *zvol_taskq;
55static kmutex_t zvol_state_lock;
56static list_t zvol_state_list;
57static char *zvol_tag = "zvol_tag";
58
59/*
60 * The in-core state of each volume.
61 */
62typedef struct zvol_state {
4c0d8e50 63 char zv_name[MAXNAMELEN]; /* name */
60101509
BB
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 */
88static int
89zvol_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 */
111static zvol_state_t *
112zvol_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 */
129static zvol_state_t *
130zvol_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)) {
4c0d8e50 137 if (!strncmp(zv->zv_name, name, MAXNAMELEN))
60101509
BB
138 return zv;
139 }
140
141 return NULL;
142}
143
144/*
145 * ZFS_IOC_CREATE callback handles dmu zvol and zap object creation.
146 */
147void
148zvol_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 */
185int
186zvol_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 */
213int
214zvol_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 */
232static int
df554c14 233zvol_update_volsize(zvol_state_t *zv, uint64_t volsize, objset_t *os)
60101509
BB
234{
235 struct block_device *bdev;
236 dmu_tx_t *tx;
237 int error;
238
239 ASSERT(MUTEX_HELD(&zvol_state_lock));
240
df554c14 241 tx = dmu_tx_create(os);
60101509
BB
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
df554c14 249 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
60101509
BB
250 &volsize, tx);
251 dmu_tx_commit(tx);
252
253 if (error)
254 return (error);
255
df554c14 256 error = dmu_free_long_range(os,
60101509
BB
257 ZVOL_OBJ, volsize, DMU_OBJECT_END);
258 if (error)
259 return (error);
260
60101509
BB
261 bdev = bdget_disk(zv->zv_disk, 0);
262 if (!bdev)
df554c14
BB
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 */
60101509 277
60101509
BB
278 bdput(bdev);
279
280 return (0);
281}
282
283/*
284 * Set ZFS_PROP_VOLSIZE set entry point.
285 */
286int
287zvol_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
df554c14 324 error = zvol_update_volsize(zv, volsize, os);
60101509
BB
325out_doi:
326 kmem_free(doi, sizeof(dmu_object_info_t));
327out:
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 */
339int
340zvol_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 */
353int
354zvol_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 }
387out:
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 */
397static int
398zvol_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
423static int
424zvol_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 */
433zil_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 */
455ssize_t zvol_immediate_write_sz = 32768;
456
457static void
458zvol_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;
ab85f845 464 ssize_t immediate_write_sz;
60101509
BB
465
466 if (zil_replaying(zilog, tx))
467 return;
468
ab85f845
ED
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);
60101509
BB
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 */
ab85f845 484 if (blocksize > immediate_write_sz && !slogging &&
60101509
BB
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 */
531static void
532zvol_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
b18019d2
ED
543 if (req->cmd_flags & VDEV_REQ_FLUSH)
544 zil_commit(zv->zv_zilog, ZVOL_OBJ);
545
546 /*
547 * Some requests are just for flush and nothing else.
548 */
549 if (size == 0) {
550 blk_end_request(req, 0, size);
551 return;
552 }
553
60101509
BB
554 rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_WRITER);
555
556 tx = dmu_tx_create(zv->zv_objset);
557 dmu_tx_hold_write(tx, ZVOL_OBJ, offset, size);
558
559 /* This will only fail for ENOSPC */
560 error = dmu_tx_assign(tx, TXG_WAIT);
561 if (error) {
562 dmu_tx_abort(tx);
563 zfs_range_unlock(rl);
564 blk_end_request(req, -error, size);
565 return;
566 }
567
568 error = dmu_write_req(zv->zv_objset, ZVOL_OBJ, req, tx);
569 if (error == 0)
b18019d2
ED
570 zvol_log_write(zv, tx, offset, size,
571 req->cmd_flags & VDEV_REQ_FUA);
60101509
BB
572
573 dmu_tx_commit(tx);
574 zfs_range_unlock(rl);
575
b18019d2
ED
576 if ((req->cmd_flags & VDEV_REQ_FUA) ||
577 zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)
60101509
BB
578 zil_commit(zv->zv_zilog, ZVOL_OBJ);
579
580 blk_end_request(req, -error, size);
581}
582
30930fba
ED
583#ifdef HAVE_BLK_QUEUE_DISCARD
584static void
585zvol_discard(void *arg)
586{
587 struct request *req = (struct request *)arg;
588 struct request_queue *q = req->q;
589 zvol_state_t *zv = q->queuedata;
590 uint64_t offset = blk_rq_pos(req) << 9;
591 uint64_t size = blk_rq_bytes(req);
592 int error;
593 rl_t *rl;
594
595 if (offset + size > zv->zv_volsize) {
596 blk_end_request(req, -EIO, size);
597 return;
598 }
599
600 if (size == 0) {
601 blk_end_request(req, 0, size);
602 return;
603 }
604
605 rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_WRITER);
606
607 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, offset, size);
608
609 /*
610 * TODO: maybe we should add the operation to the log.
611 */
612
613 zfs_range_unlock(rl);
614
615 blk_end_request(req, -error, size);
616}
617#endif /* HAVE_BLK_QUEUE_DISCARD */
618
60101509
BB
619/*
620 * Common read path running under the zvol taskq context. This function
621 * is responsible for copying the requested data out of the DMU and in to
622 * a linux request structure. It then must signal the request queue with
623 * an error code describing the result of the copy.
624 */
625static void
626zvol_read(void *arg)
627{
628 struct request *req = (struct request *)arg;
629 struct request_queue *q = req->q;
630 zvol_state_t *zv = q->queuedata;
631 uint64_t offset = blk_rq_pos(req) << 9;
632 uint64_t size = blk_rq_bytes(req);
633 int error;
634 rl_t *rl;
635
b18019d2
ED
636 if (size == 0) {
637 blk_end_request(req, 0, size);
638 return;
639 }
640
60101509
BB
641 rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
642
643 error = dmu_read_req(zv->zv_objset, ZVOL_OBJ, req);
644
645 zfs_range_unlock(rl);
646
647 /* convert checksum errors into IO errors */
648 if (error == ECKSUM)
649 error = EIO;
650
651 blk_end_request(req, -error, size);
652}
653
654/*
655 * Request will be added back to the request queue and retried if
656 * it cannot be immediately dispatched to the taskq for handling
657 */
658static inline void
659zvol_dispatch(task_func_t func, struct request *req)
660{
661 if (!taskq_dispatch(zvol_taskq, func, (void *)req, TQ_NOSLEEP))
662 blk_requeue_request(req->q, req);
663}
664
665/*
666 * Common request path. Rather than registering a custom make_request()
667 * function we use the generic Linux version. This is done because it allows
668 * us to easily merge read requests which would otherwise we performed
669 * synchronously by the DMU. This is less critical in write case where the
670 * DMU will perform the correct merging within a transaction group. Using
671 * the generic make_request() also let's use leverage the fact that the
672 * elevator with ensure correct ordering in regards to barrior IOs. On
673 * the downside it means that in the write case we end up doing request
674 * merging twice once in the elevator and once in the DMU.
675 *
676 * The request handler is called under a spin lock so all the real work
677 * is handed off to be done in the context of the zvol taskq. This function
678 * simply performs basic request sanity checking and hands off the request.
679 */
680static void
681zvol_request(struct request_queue *q)
682{
683 zvol_state_t *zv = q->queuedata;
684 struct request *req;
685 unsigned int size;
686
687 while ((req = blk_fetch_request(q)) != NULL) {
688 size = blk_rq_bytes(req);
689
b18019d2 690 if (size != 0 && blk_rq_pos(req) + blk_rq_sectors(req) >
60101509
BB
691 get_capacity(zv->zv_disk)) {
692 printk(KERN_INFO
693 "%s: bad access: block=%llu, count=%lu\n",
694 req->rq_disk->disk_name,
695 (long long unsigned)blk_rq_pos(req),
696 (long unsigned)blk_rq_sectors(req));
697 __blk_end_request(req, -EIO, size);
698 continue;
699 }
700
701 if (!blk_fs_request(req)) {
702 printk(KERN_INFO "%s: non-fs cmd\n",
703 req->rq_disk->disk_name);
704 __blk_end_request(req, -EIO, size);
705 continue;
706 }
707
708 switch (rq_data_dir(req)) {
709 case READ:
710 zvol_dispatch(zvol_read, req);
711 break;
712 case WRITE:
713 if (unlikely(get_disk_ro(zv->zv_disk)) ||
714 unlikely(zv->zv_flags & ZVOL_RDONLY)) {
715 __blk_end_request(req, -EROFS, size);
716 break;
717 }
718
30930fba
ED
719#ifdef HAVE_BLK_QUEUE_DISCARD
720 if (req->cmd_flags & VDEV_REQ_DISCARD) {
721 zvol_dispatch(zvol_discard, req);
722 break;
723 }
724#endif /* HAVE_BLK_QUEUE_DISCARD */
725
60101509
BB
726 zvol_dispatch(zvol_write, req);
727 break;
728 default:
729 printk(KERN_INFO "%s: unknown cmd: %d\n",
730 req->rq_disk->disk_name, (int)rq_data_dir(req));
731 __blk_end_request(req, -EIO, size);
732 break;
733 }
734 }
735}
736
737static void
738zvol_get_done(zgd_t *zgd, int error)
739{
740 if (zgd->zgd_db)
741 dmu_buf_rele(zgd->zgd_db, zgd);
742
743 zfs_range_unlock(zgd->zgd_rl);
744
745 if (error == 0 && zgd->zgd_bp)
746 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
747
748 kmem_free(zgd, sizeof (zgd_t));
749}
750
751/*
752 * Get data to generate a TX_WRITE intent log record.
753 */
754static int
755zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
756{
757 zvol_state_t *zv = arg;
758 objset_t *os = zv->zv_objset;
759 uint64_t offset = lr->lr_offset;
760 uint64_t size = lr->lr_length;
761 dmu_buf_t *db;
762 zgd_t *zgd;
763 int error;
764
765 ASSERT(zio != NULL);
766 ASSERT(size != 0);
767
768 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
769 zgd->zgd_zilog = zv->zv_zilog;
770 zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
771
772 /*
773 * Write records come in two flavors: immediate and indirect.
774 * For small writes it's cheaper to store the data with the
775 * log record (immediate); for large writes it's cheaper to
776 * sync the data and get a pointer to it (indirect) so that
777 * we don't have to write the data twice.
778 */
779 if (buf != NULL) { /* immediate write */
780 error = dmu_read(os, ZVOL_OBJ, offset, size, buf,
781 DMU_READ_NO_PREFETCH);
782 } else {
783 size = zv->zv_volblocksize;
784 offset = P2ALIGN_TYPED(offset, size, uint64_t);
785 error = dmu_buf_hold(os, ZVOL_OBJ, offset, zgd, &db,
786 DMU_READ_NO_PREFETCH);
787 if (error == 0) {
788 zgd->zgd_db = db;
789 zgd->zgd_bp = &lr->lr_blkptr;
790
791 ASSERT(db != NULL);
792 ASSERT(db->db_offset == offset);
793 ASSERT(db->db_size == size);
794
795 error = dmu_sync(zio, lr->lr_common.lrc_txg,
796 zvol_get_done, zgd);
797
798 if (error == 0)
799 return (0);
800 }
801 }
802
803 zvol_get_done(zgd, error);
804
805 return (error);
806}
807
808/*
809 * The zvol_state_t's are inserted in increasing MINOR(dev_t) order.
810 */
811static void
812zvol_insert(zvol_state_t *zv_insert)
813{
814 zvol_state_t *zv = NULL;
815
816 ASSERT(MUTEX_HELD(&zvol_state_lock));
817 ASSERT3U(MINOR(zv_insert->zv_dev) & ZVOL_MINOR_MASK, ==, 0);
818 for (zv = list_head(&zvol_state_list); zv != NULL;
819 zv = list_next(&zvol_state_list, zv)) {
820 if (MINOR(zv->zv_dev) > MINOR(zv_insert->zv_dev))
821 break;
822 }
823
824 list_insert_before(&zvol_state_list, zv, zv_insert);
825}
826
827/*
828 * Simply remove the zvol from to list of zvols.
829 */
830static void
831zvol_remove(zvol_state_t *zv_remove)
832{
833 ASSERT(MUTEX_HELD(&zvol_state_lock));
834 list_remove(&zvol_state_list, zv_remove);
835}
836
837static int
838zvol_first_open(zvol_state_t *zv)
839{
840 objset_t *os;
841 uint64_t volsize;
842 int error;
843 uint64_t ro;
844
845 /* lie and say we're read-only */
846 error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, 1, zvol_tag, &os);
847 if (error)
848 return (-error);
849
850 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
851 if (error) {
852 dmu_objset_disown(os, zvol_tag);
853 return (-error);
854 }
855
856 zv->zv_objset = os;
857 error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf);
858 if (error) {
859 dmu_objset_disown(os, zvol_tag);
860 return (-error);
861 }
862
863 set_capacity(zv->zv_disk, volsize >> 9);
864 zv->zv_volsize = volsize;
865 zv->zv_zilog = zil_open(os, zvol_get_data);
866
867 VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &ro, NULL) == 0);
868 if (ro || dmu_objset_is_snapshot(os)) {
869 set_disk_ro(zv->zv_disk, 1);
870 zv->zv_flags |= ZVOL_RDONLY;
871 } else {
872 set_disk_ro(zv->zv_disk, 0);
873 zv->zv_flags &= ~ZVOL_RDONLY;
874 }
875
876 return (-error);
877}
878
879static void
880zvol_last_close(zvol_state_t *zv)
881{
882 zil_close(zv->zv_zilog);
883 zv->zv_zilog = NULL;
884 dmu_buf_rele(zv->zv_dbuf, zvol_tag);
885 zv->zv_dbuf = NULL;
886 dmu_objset_disown(zv->zv_objset, zvol_tag);
887 zv->zv_objset = NULL;
888}
889
890static int
891zvol_open(struct block_device *bdev, fmode_t flag)
892{
893 zvol_state_t *zv = bdev->bd_disk->private_data;
894 int error = 0, drop_mutex = 0;
895
896 /*
897 * If the caller is already holding the mutex do not take it
898 * again, this will happen as part of zvol_create_minor().
899 * Once add_disk() is called the device is live and the kernel
900 * will attempt to open it to read the partition information.
901 */
902 if (!mutex_owned(&zvol_state_lock)) {
903 mutex_enter(&zvol_state_lock);
904 drop_mutex = 1;
905 }
906
907 ASSERT3P(zv, !=, NULL);
908
909 if (zv->zv_open_count == 0) {
910 error = zvol_first_open(zv);
911 if (error)
912 goto out_mutex;
913 }
914
915 if ((flag & FMODE_WRITE) &&
916 (get_disk_ro(zv->zv_disk) || (zv->zv_flags & ZVOL_RDONLY))) {
917 error = -EROFS;
918 goto out_open_count;
919 }
920
921 zv->zv_open_count++;
922
923out_open_count:
924 if (zv->zv_open_count == 0)
925 zvol_last_close(zv);
926
927out_mutex:
928 if (drop_mutex)
929 mutex_exit(&zvol_state_lock);
930
931 check_disk_change(bdev);
932
933 return (error);
934}
935
936static int
937zvol_release(struct gendisk *disk, fmode_t mode)
938{
939 zvol_state_t *zv = disk->private_data;
940 int drop_mutex = 0;
941
942 if (!mutex_owned(&zvol_state_lock)) {
943 mutex_enter(&zvol_state_lock);
944 drop_mutex = 1;
945 }
946
947 ASSERT3P(zv, !=, NULL);
948 ASSERT3U(zv->zv_open_count, >, 0);
949 zv->zv_open_count--;
950 if (zv->zv_open_count == 0)
951 zvol_last_close(zv);
952
953 if (drop_mutex)
954 mutex_exit(&zvol_state_lock);
955
956 return (0);
957}
958
959static int
960zvol_ioctl(struct block_device *bdev, fmode_t mode,
961 unsigned int cmd, unsigned long arg)
962{
963 zvol_state_t *zv = bdev->bd_disk->private_data;
964 int error = 0;
965
966 if (zv == NULL)
967 return (-ENXIO);
968
969 switch (cmd) {
970 case BLKFLSBUF:
971 zil_commit(zv->zv_zilog, ZVOL_OBJ);
972 break;
4c0d8e50
FN
973 case BLKZNAME:
974 error = copy_to_user((void *)arg, zv->zv_name, MAXNAMELEN);
975 break;
60101509
BB
976
977 default:
978 error = -ENOTTY;
979 break;
980
981 }
982
983 return (error);
984}
985
986#ifdef CONFIG_COMPAT
987static int
988zvol_compat_ioctl(struct block_device *bdev, fmode_t mode,
989 unsigned cmd, unsigned long arg)
990{
991 return zvol_ioctl(bdev, mode, cmd, arg);
992}
993#else
994#define zvol_compat_ioctl NULL
995#endif
996
997static int zvol_media_changed(struct gendisk *disk)
998{
999 zvol_state_t *zv = disk->private_data;
1000
1001 return zv->zv_changed;
1002}
1003
1004static int zvol_revalidate_disk(struct gendisk *disk)
1005{
1006 zvol_state_t *zv = disk->private_data;
1007
1008 zv->zv_changed = 0;
1009 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1010
1011 return 0;
1012}
1013
1014/*
1015 * Provide a simple virtual geometry for legacy compatibility. For devices
1016 * smaller than 1 MiB a small head and sector count is used to allow very
1017 * tiny devices. For devices over 1 Mib a standard head and sector count
1018 * is used to keep the cylinders count reasonable.
1019 */
1020static int
1021zvol_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1022{
1023 zvol_state_t *zv = bdev->bd_disk->private_data;
1024 sector_t sectors = get_capacity(zv->zv_disk);
1025
1026 if (sectors > 2048) {
1027 geo->heads = 16;
1028 geo->sectors = 63;
1029 } else {
1030 geo->heads = 2;
1031 geo->sectors = 4;
1032 }
1033
1034 geo->start = 0;
1035 geo->cylinders = sectors / (geo->heads * geo->sectors);
1036
1037 return 0;
1038}
1039
1040static struct kobject *
1041zvol_probe(dev_t dev, int *part, void *arg)
1042{
1043 zvol_state_t *zv;
1044 struct kobject *kobj;
1045
1046 mutex_enter(&zvol_state_lock);
1047 zv = zvol_find_by_dev(dev);
1048 kobj = zv ? get_disk(zv->zv_disk) : ERR_PTR(-ENOENT);
1049 mutex_exit(&zvol_state_lock);
1050
1051 return kobj;
1052}
1053
1054#ifdef HAVE_BDEV_BLOCK_DEVICE_OPERATIONS
1055static struct block_device_operations zvol_ops = {
1056 .open = zvol_open,
1057 .release = zvol_release,
1058 .ioctl = zvol_ioctl,
1059 .compat_ioctl = zvol_compat_ioctl,
1060 .media_changed = zvol_media_changed,
1061 .revalidate_disk = zvol_revalidate_disk,
1062 .getgeo = zvol_getgeo,
1063 .owner = THIS_MODULE,
1064};
1065
1066#else /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1067
1068static int
1069zvol_open_by_inode(struct inode *inode, struct file *file)
1070{
1071 return zvol_open(inode->i_bdev, file->f_mode);
1072}
1073
1074static int
1075zvol_release_by_inode(struct inode *inode, struct file *file)
1076{
1077 return zvol_release(inode->i_bdev->bd_disk, file->f_mode);
1078}
1079
1080static int
1081zvol_ioctl_by_inode(struct inode *inode, struct file *file,
1082 unsigned int cmd, unsigned long arg)
1083{
b1c58213
NB
1084 if (file == NULL || inode == NULL)
1085 return -EINVAL;
60101509
BB
1086 return zvol_ioctl(inode->i_bdev, file->f_mode, cmd, arg);
1087}
1088
1089# ifdef CONFIG_COMPAT
1090static long
1091zvol_compat_ioctl_by_inode(struct file *file,
1092 unsigned int cmd, unsigned long arg)
1093{
b1c58213
NB
1094 if (file == NULL)
1095 return -EINVAL;
60101509
BB
1096 return zvol_compat_ioctl(file->f_dentry->d_inode->i_bdev,
1097 file->f_mode, cmd, arg);
1098}
1099# else
1100# define zvol_compat_ioctl_by_inode NULL
1101# endif
1102
1103static struct block_device_operations zvol_ops = {
1104 .open = zvol_open_by_inode,
1105 .release = zvol_release_by_inode,
1106 .ioctl = zvol_ioctl_by_inode,
1107 .compat_ioctl = zvol_compat_ioctl_by_inode,
1108 .media_changed = zvol_media_changed,
1109 .revalidate_disk = zvol_revalidate_disk,
1110 .getgeo = zvol_getgeo,
1111 .owner = THIS_MODULE,
1112};
1113#endif /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1114
1115/*
1116 * Allocate memory for a new zvol_state_t and setup the required
1117 * request queue and generic disk structures for the block device.
1118 */
1119static zvol_state_t *
1120zvol_alloc(dev_t dev, const char *name)
1121{
1122 zvol_state_t *zv;
1123
1124 zv = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
1125 if (zv == NULL)
1126 goto out;
1127
1128 zv->zv_queue = blk_init_queue(zvol_request, &zv->zv_lock);
1129 if (zv->zv_queue == NULL)
1130 goto out_kmem;
1131
b18019d2
ED
1132#ifdef HAVE_BLK_QUEUE_FLUSH
1133 blk_queue_flush(zv->zv_queue, VDEV_REQ_FLUSH | VDEV_REQ_FUA);
1134#else
1135 blk_queue_ordered(zv->zv_queue, QUEUE_ORDERED_DRAIN, NULL);
1136#endif /* HAVE_BLK_QUEUE_FLUSH */
1137
60101509
BB
1138 zv->zv_disk = alloc_disk(ZVOL_MINORS);
1139 if (zv->zv_disk == NULL)
1140 goto out_queue;
1141
1142 zv->zv_queue->queuedata = zv;
1143 zv->zv_dev = dev;
1144 zv->zv_open_count = 0;
4c0d8e50 1145 strlcpy(zv->zv_name, name, MAXNAMELEN);
60101509
BB
1146
1147 mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
1148 avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
1149 sizeof (rl_t), offsetof(rl_t, r_node));
3c4988c8
BB
1150 zv->zv_znode.z_is_zvol = TRUE;
1151
60101509
BB
1152 spin_lock_init(&zv->zv_lock);
1153 list_link_init(&zv->zv_next);
1154
1155 zv->zv_disk->major = zvol_major;
1156 zv->zv_disk->first_minor = (dev & MINORMASK);
1157 zv->zv_disk->fops = &zvol_ops;
1158 zv->zv_disk->private_data = zv;
1159 zv->zv_disk->queue = zv->zv_queue;
4c0d8e50
FN
1160 snprintf(zv->zv_disk->disk_name, DISK_NAME_LEN, "%s%d",
1161 ZVOL_DEV_NAME, (dev & MINORMASK));
60101509
BB
1162
1163 return zv;
1164
1165out_queue:
1166 blk_cleanup_queue(zv->zv_queue);
1167out_kmem:
1168 kmem_free(zv, sizeof (zvol_state_t));
1169out:
1170 return NULL;
1171}
1172
1173/*
1174 * Cleanup then free a zvol_state_t which was created by zvol_alloc().
1175 */
1176static void
1177zvol_free(zvol_state_t *zv)
1178{
1179 avl_destroy(&zv->zv_znode.z_range_avl);
1180 mutex_destroy(&zv->zv_znode.z_range_lock);
1181
1182 del_gendisk(zv->zv_disk);
1183 blk_cleanup_queue(zv->zv_queue);
1184 put_disk(zv->zv_disk);
1185
1186 kmem_free(zv, sizeof (zvol_state_t));
1187}
1188
1189static int
1190__zvol_create_minor(const char *name)
1191{
1192 zvol_state_t *zv;
1193 objset_t *os;
1194 dmu_object_info_t *doi;
1195 uint64_t volsize;
1196 unsigned minor = 0;
1197 int error = 0;
1198
1199 ASSERT(MUTEX_HELD(&zvol_state_lock));
1200
1201 zv = zvol_find_by_name(name);
1202 if (zv) {
1203 error = EEXIST;
1204 goto out;
1205 }
1206
1207 doi = kmem_alloc(sizeof(dmu_object_info_t), KM_SLEEP);
1208
1209 error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os);
1210 if (error)
1211 goto out_doi;
1212
1213 error = dmu_object_info(os, ZVOL_OBJ, doi);
1214 if (error)
1215 goto out_dmu_objset_disown;
1216
1217 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
1218 if (error)
1219 goto out_dmu_objset_disown;
1220
1221 error = zvol_find_minor(&minor);
1222 if (error)
1223 goto out_dmu_objset_disown;
1224
1225 zv = zvol_alloc(MKDEV(zvol_major, minor), name);
1226 if (zv == NULL) {
1227 error = EAGAIN;
1228 goto out_dmu_objset_disown;
1229 }
1230
1231 if (dmu_objset_is_snapshot(os))
1232 zv->zv_flags |= ZVOL_RDONLY;
1233
1234 zv->zv_volblocksize = doi->doi_data_block_size;
1235 zv->zv_volsize = volsize;
1236 zv->zv_objset = os;
1237
1238 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1239
34037afe
ED
1240 blk_queue_max_hw_sectors(zv->zv_queue, UINT_MAX);
1241 blk_queue_max_segments(zv->zv_queue, UINT16_MAX);
1242 blk_queue_max_segment_size(zv->zv_queue, UINT_MAX);
1243 blk_queue_physical_block_size(zv->zv_queue, zv->zv_volblocksize);
1244 blk_queue_io_opt(zv->zv_queue, zv->zv_volblocksize);
30930fba 1245#ifdef HAVE_BLK_QUEUE_DISCARD
7c0e5708
ED
1246 blk_queue_max_discard_sectors(zv->zv_queue,
1247 (zvol_max_discard_blocks * zv->zv_volblocksize) >> 9);
30930fba
ED
1248 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, zv->zv_queue);
1249#endif
34037afe
ED
1250#ifdef HAVE_BLK_QUEUE_NONROT
1251 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zv->zv_queue);
1252#endif
1253
60101509
BB
1254 if (zil_replay_disable)
1255 zil_destroy(dmu_objset_zil(os), B_FALSE);
1256 else
1257 zil_replay(os, zv, zvol_replay_vector);
1258
1259out_dmu_objset_disown:
1260 dmu_objset_disown(os, zvol_tag);
1261 zv->zv_objset = NULL;
1262out_doi:
1263 kmem_free(doi, sizeof(dmu_object_info_t));
1264out:
1265
1266 if (error == 0) {
1267 zvol_insert(zv);
1268 add_disk(zv->zv_disk);
1269 }
1270
1271 return (error);
1272}
1273
1274/*
1275 * Create a block device minor node and setup the linkage between it
1276 * and the specified volume. Once this function returns the block
1277 * device is live and ready for use.
1278 */
1279int
1280zvol_create_minor(const char *name)
1281{
1282 int error;
1283
1284 mutex_enter(&zvol_state_lock);
1285 error = __zvol_create_minor(name);
1286 mutex_exit(&zvol_state_lock);
1287
1288 return (error);
1289}
1290
1291static int
1292__zvol_remove_minor(const char *name)
1293{
1294 zvol_state_t *zv;
1295
1296 ASSERT(MUTEX_HELD(&zvol_state_lock));
1297
1298 zv = zvol_find_by_name(name);
1299 if (zv == NULL)
1300 return (ENXIO);
1301
1302 if (zv->zv_open_count > 0)
1303 return (EBUSY);
1304
1305 zvol_remove(zv);
1306 zvol_free(zv);
1307
1308 return (0);
1309}
1310
1311/*
1312 * Remove a block device minor node for the specified volume.
1313 */
1314int
1315zvol_remove_minor(const char *name)
1316{
1317 int error;
1318
1319 mutex_enter(&zvol_state_lock);
1320 error = __zvol_remove_minor(name);
1321 mutex_exit(&zvol_state_lock);
1322
1323 return (error);
1324}
1325
1326static int
1327zvol_create_minors_cb(spa_t *spa, uint64_t dsobj,
1328 const char *dsname, void *arg)
1329{
1330 if (strchr(dsname, '/') == NULL)
1331 return 0;
1332
d5674448
BB
1333 (void) __zvol_create_minor(dsname);
1334 return (0);
60101509
BB
1335}
1336
1337/*
1338 * Create minors for specified pool, if pool is NULL create minors
1339 * for all available pools.
1340 */
1341int
1342zvol_create_minors(const char *pool)
1343{
1344 spa_t *spa = NULL;
1345 int error = 0;
1346
74497b7a
DH
1347 if (zvol_inhibit_dev)
1348 return (0);
1349
60101509
BB
1350 mutex_enter(&zvol_state_lock);
1351 if (pool) {
1352 error = dmu_objset_find_spa(NULL, pool, zvol_create_minors_cb,
1353 NULL, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1354 } else {
1355 mutex_enter(&spa_namespace_lock);
1356 while ((spa = spa_next(spa)) != NULL) {
1357 error = dmu_objset_find_spa(NULL,
1358 spa_name(spa), zvol_create_minors_cb, NULL,
1359 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1360 if (error)
1361 break;
1362 }
1363 mutex_exit(&spa_namespace_lock);
1364 }
1365 mutex_exit(&zvol_state_lock);
1366
1367 return error;
1368}
1369
1370/*
1371 * Remove minors for specified pool, if pool is NULL remove all minors.
1372 */
1373void
1374zvol_remove_minors(const char *pool)
1375{
1376 zvol_state_t *zv, *zv_next;
1377 char *str;
1378
74497b7a
DH
1379 if (zvol_inhibit_dev)
1380 return;
1381
4c0d8e50 1382 str = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
60101509
BB
1383 if (pool) {
1384 (void) strncpy(str, pool, strlen(pool));
1385 (void) strcat(str, "/");
1386 }
1387
1388 mutex_enter(&zvol_state_lock);
1389 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1390 zv_next = list_next(&zvol_state_list, zv);
1391
1392 if (pool == NULL || !strncmp(str, zv->zv_name, strlen(str))) {
1393 zvol_remove(zv);
1394 zvol_free(zv);
1395 }
1396 }
1397 mutex_exit(&zvol_state_lock);
4c0d8e50 1398 kmem_free(str, MAXNAMELEN);
60101509
BB
1399}
1400
1401int
1402zvol_init(void)
1403{
1404 int error;
1405
60101509 1406 zvol_taskq = taskq_create(ZVOL_DRIVER, zvol_threads, maxclsyspri,
71011408 1407 zvol_threads, INT_MAX, TASKQ_PREPOPULATE);
60101509
BB
1408 if (zvol_taskq == NULL) {
1409 printk(KERN_INFO "ZFS: taskq_create() failed\n");
1410 return (-ENOMEM);
1411 }
1412
1413 error = register_blkdev(zvol_major, ZVOL_DRIVER);
1414 if (error) {
1415 printk(KERN_INFO "ZFS: register_blkdev() failed %d\n", error);
1416 taskq_destroy(zvol_taskq);
1417 return (error);
1418 }
1419
1420 blk_register_region(MKDEV(zvol_major, 0), 1UL << MINORBITS,
1421 THIS_MODULE, zvol_probe, NULL, NULL);
1422
1423 mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
1424 list_create(&zvol_state_list, sizeof (zvol_state_t),
1425 offsetof(zvol_state_t, zv_next));
1426
1427 (void) zvol_create_minors(NULL);
1428
1429 return (0);
1430}
1431
1432void
1433zvol_fini(void)
1434{
1435 zvol_remove_minors(NULL);
1436 blk_unregister_region(MKDEV(zvol_major, 0), 1UL << MINORBITS);
1437 unregister_blkdev(zvol_major, ZVOL_DRIVER);
1438 taskq_destroy(zvol_taskq);
1439 mutex_destroy(&zvol_state_lock);
1440 list_destroy(&zvol_state_list);
1441}
1442
74497b7a
DH
1443module_param(zvol_inhibit_dev, uint, 0644);
1444MODULE_PARM_DESC(zvol_inhibit_dev, "Do not create zvol device nodes");
1445
30a9524e 1446module_param(zvol_major, uint, 0444);
60101509
BB
1447MODULE_PARM_DESC(zvol_major, "Major number for zvol device");
1448
30a9524e 1449module_param(zvol_threads, uint, 0444);
60101509 1450MODULE_PARM_DESC(zvol_threads, "Number of threads for zvol device");
7c0e5708
ED
1451
1452module_param(zvol_max_discard_blocks, ulong, 0444);
1453MODULE_PARM_DESC(zvol_max_discard_blocks, "Max number of blocks to discard at once");