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