]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zvol.c
8bc0a608ced5605e8d3d6df799126f67521a15e2
[mirror_zfs.git] / module / zfs / zvol.c
1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright (C) 2008-2010 Lawrence Livermore National Security, LLC.
23 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
24 * Rewritten for Linux by Brian Behlendorf <behlendorf1@llnl.gov>.
25 * LLNL-CODE-403049.
26 *
27 * ZFS volume emulation driver.
28 *
29 * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes.
30 * Volumes are accessed through the symbolic links named:
31 *
32 * /dev/<pool_name>/<dataset_name>
33 *
34 * Volumes are persistent through reboot and module load. No user command
35 * needs to be run before opening and using a device.
36 *
37 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
38 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
39 */
40
41 #include <sys/dbuf.h>
42 #include <sys/dmu_traverse.h>
43 #include <sys/dsl_dataset.h>
44 #include <sys/dsl_prop.h>
45 #include <sys/dsl_dir.h>
46 #include <sys/zap.h>
47 #include <sys/zfeature.h>
48 #include <sys/zil_impl.h>
49 #include <sys/dmu_tx.h>
50 #include <sys/zio.h>
51 #include <sys/zfs_rlock.h>
52 #include <sys/zfs_znode.h>
53 #include <sys/spa_impl.h>
54 #include <sys/zvol.h>
55 #include <linux/blkdev_compat.h>
56
57 unsigned int zvol_inhibit_dev = 0;
58 unsigned int zvol_major = ZVOL_MAJOR;
59 unsigned int zvol_prefetch_bytes = (128 * 1024);
60 unsigned long zvol_max_discard_blocks = 16384;
61
62 static kmutex_t zvol_state_lock;
63 static list_t zvol_state_list;
64 void *zvol_tag = "zvol_tag";
65
66 #define ZVOL_HT_SIZE 1024
67 static struct hlist_head *zvol_htable;
68 #define ZVOL_HT_HEAD(hash) (&zvol_htable[(hash) & (ZVOL_HT_SIZE-1)])
69 static DEFINE_IDA(zvol_ida);
70
71 /*
72 * The in-core state of each volume.
73 */
74 typedef struct zvol_state {
75 char zv_name[MAXNAMELEN]; /* name */
76 uint64_t zv_volsize; /* advertised space */
77 uint64_t zv_volblocksize; /* volume block size */
78 objset_t *zv_objset; /* objset handle */
79 uint32_t zv_flags; /* ZVOL_* flags */
80 uint32_t zv_open_count; /* open counts */
81 uint32_t zv_changed; /* disk changed */
82 zilog_t *zv_zilog; /* ZIL handle */
83 zfs_rlock_t zv_range_lock; /* range lock */
84 dmu_buf_t *zv_dbuf; /* bonus handle */
85 dev_t zv_dev; /* device id */
86 struct gendisk *zv_disk; /* generic disk */
87 struct request_queue *zv_queue; /* request queue */
88 list_node_t zv_next; /* next zvol_state_t linkage */
89 uint64_t zv_hash; /* name hash */
90 struct hlist_node zv_hlink; /* hash link */
91 } zvol_state_t;
92
93 typedef enum {
94 ZVOL_ASYNC_CREATE_MINORS,
95 ZVOL_ASYNC_REMOVE_MINORS,
96 ZVOL_ASYNC_RENAME_MINORS,
97 ZVOL_ASYNC_SET_SNAPDEV,
98 ZVOL_ASYNC_MAX
99 } zvol_async_op_t;
100
101 typedef struct {
102 zvol_async_op_t op;
103 char pool[MAXNAMELEN];
104 char name1[MAXNAMELEN];
105 char name2[MAXNAMELEN];
106 zprop_source_t source;
107 uint64_t snapdev;
108 } zvol_task_t;
109
110 #define ZVOL_RDONLY 0x1
111
112 static uint64_t
113 zvol_name_hash(const char *name)
114 {
115 int i;
116 uint64_t crc = -1ULL;
117 uint8_t *p = (uint8_t *)name;
118 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
119 for (i = 0; i < MAXNAMELEN - 1 && *p; i++, p++) {
120 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (*p)) & 0xFF];
121 }
122 return (crc);
123 }
124
125 /*
126 * Find a zvol_state_t given the full major+minor dev_t.
127 */
128 static zvol_state_t *
129 zvol_find_by_dev(dev_t dev)
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 (zv->zv_dev == dev)
137 return (zv);
138 }
139
140 return (NULL);
141 }
142
143 /*
144 * Find a zvol_state_t given the name and hash generated by zvol_name_hash.
145 */
146 static zvol_state_t *
147 zvol_find_by_name_hash(const char *name, uint64_t hash)
148 {
149 zvol_state_t *zv;
150 struct hlist_node *p;
151
152 ASSERT(MUTEX_HELD(&zvol_state_lock));
153 hlist_for_each(p, ZVOL_HT_HEAD(hash)) {
154 zv = hlist_entry(p, zvol_state_t, zv_hlink);
155 if (zv->zv_hash == hash &&
156 strncmp(zv->zv_name, name, MAXNAMELEN) == 0)
157 return (zv);
158 }
159 return (NULL);
160 }
161
162 /*
163 * Find a zvol_state_t given the name provided at zvol_alloc() time.
164 */
165 static zvol_state_t *
166 zvol_find_by_name(const char *name)
167 {
168 return (zvol_find_by_name_hash(name, zvol_name_hash(name)));
169 }
170
171
172 /*
173 * Given a path, return TRUE if path is a ZVOL.
174 */
175 boolean_t
176 zvol_is_zvol(const char *device)
177 {
178 struct block_device *bdev;
179 unsigned int major;
180
181 bdev = vdev_lookup_bdev(device);
182 if (IS_ERR(bdev))
183 return (B_FALSE);
184
185 major = MAJOR(bdev->bd_dev);
186 bdput(bdev);
187
188 if (major == zvol_major)
189 return (B_TRUE);
190
191 return (B_FALSE);
192 }
193
194 /*
195 * ZFS_IOC_CREATE callback handles dmu zvol and zap object creation.
196 */
197 void
198 zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
199 {
200 zfs_creat_t *zct = arg;
201 nvlist_t *nvprops = zct->zct_props;
202 int error;
203 uint64_t volblocksize, volsize;
204
205 VERIFY(nvlist_lookup_uint64(nvprops,
206 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
207 if (nvlist_lookup_uint64(nvprops,
208 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
209 volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
210
211 /*
212 * These properties must be removed from the list so the generic
213 * property setting step won't apply to them.
214 */
215 VERIFY(nvlist_remove_all(nvprops,
216 zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
217 (void) nvlist_remove_all(nvprops,
218 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
219
220 error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
221 DMU_OT_NONE, 0, tx);
222 ASSERT(error == 0);
223
224 error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
225 DMU_OT_NONE, 0, tx);
226 ASSERT(error == 0);
227
228 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
229 ASSERT(error == 0);
230 }
231
232 /*
233 * ZFS_IOC_OBJSET_STATS entry point.
234 */
235 int
236 zvol_get_stats(objset_t *os, nvlist_t *nv)
237 {
238 int error;
239 dmu_object_info_t *doi;
240 uint64_t val;
241
242 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
243 if (error)
244 return (SET_ERROR(error));
245
246 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
247 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
248 error = dmu_object_info(os, ZVOL_OBJ, doi);
249
250 if (error == 0) {
251 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
252 doi->doi_data_block_size);
253 }
254
255 kmem_free(doi, sizeof (dmu_object_info_t));
256
257 return (SET_ERROR(error));
258 }
259
260 static void
261 zvol_size_changed(zvol_state_t *zv, uint64_t volsize)
262 {
263 struct block_device *bdev;
264
265 bdev = bdget_disk(zv->zv_disk, 0);
266 if (bdev == NULL)
267 return;
268 set_capacity(zv->zv_disk, volsize >> 9);
269 zv->zv_volsize = volsize;
270 check_disk_size_change(zv->zv_disk, bdev);
271
272 bdput(bdev);
273 }
274
275 /*
276 * Sanity check volume size.
277 */
278 int
279 zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
280 {
281 if (volsize == 0)
282 return (SET_ERROR(EINVAL));
283
284 if (volsize % blocksize != 0)
285 return (SET_ERROR(EINVAL));
286
287 #ifdef _ILP32
288 if (volsize - 1 > SPEC_MAXOFFSET_T)
289 return (SET_ERROR(EOVERFLOW));
290 #endif
291 return (0);
292 }
293
294 /*
295 * Ensure the zap is flushed then inform the VFS of the capacity change.
296 */
297 static int
298 zvol_update_volsize(uint64_t volsize, objset_t *os)
299 {
300 dmu_tx_t *tx;
301 int error;
302 uint64_t txg;
303
304 ASSERT(MUTEX_HELD(&zvol_state_lock));
305
306 tx = dmu_tx_create(os);
307 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
308 dmu_tx_mark_netfree(tx);
309 error = dmu_tx_assign(tx, TXG_WAIT);
310 if (error) {
311 dmu_tx_abort(tx);
312 return (SET_ERROR(error));
313 }
314 txg = dmu_tx_get_txg(tx);
315
316 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
317 &volsize, tx);
318 dmu_tx_commit(tx);
319
320 txg_wait_synced(dmu_objset_pool(os), txg);
321
322 if (error == 0)
323 error = dmu_free_long_range(os,
324 ZVOL_OBJ, volsize, DMU_OBJECT_END);
325
326 return (error);
327 }
328
329 static int
330 zvol_update_live_volsize(zvol_state_t *zv, uint64_t volsize)
331 {
332 zvol_size_changed(zv, volsize);
333
334 /*
335 * We should post a event here describing the expansion. However,
336 * the zfs_ereport_post() interface doesn't nicely support posting
337 * events for zvols, it assumes events relate to vdevs or zios.
338 */
339
340 return (0);
341 }
342
343 /*
344 * Set ZFS_PROP_VOLSIZE set entry point.
345 */
346 int
347 zvol_set_volsize(const char *name, uint64_t volsize)
348 {
349 zvol_state_t *zv = NULL;
350 objset_t *os = NULL;
351 int error;
352 dmu_object_info_t *doi;
353 uint64_t readonly;
354 boolean_t owned = B_FALSE;
355
356 error = dsl_prop_get_integer(name,
357 zfs_prop_to_name(ZFS_PROP_READONLY), &readonly, NULL);
358 if (error != 0)
359 return (SET_ERROR(error));
360 if (readonly)
361 return (SET_ERROR(EROFS));
362
363 mutex_enter(&zvol_state_lock);
364 zv = zvol_find_by_name(name);
365
366 if (zv == NULL || zv->zv_objset == NULL) {
367 if ((error = dmu_objset_own(name, DMU_OST_ZVOL, B_FALSE,
368 FTAG, &os)) != 0) {
369 mutex_exit(&zvol_state_lock);
370 return (SET_ERROR(error));
371 }
372 owned = B_TRUE;
373 if (zv != NULL)
374 zv->zv_objset = os;
375 } else {
376 os = zv->zv_objset;
377 }
378
379 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
380
381 if ((error = dmu_object_info(os, ZVOL_OBJ, doi)) ||
382 (error = zvol_check_volsize(volsize, doi->doi_data_block_size)))
383 goto out;
384
385 error = zvol_update_volsize(volsize, os);
386 kmem_free(doi, sizeof (dmu_object_info_t));
387
388 if (error == 0 && zv != NULL)
389 error = zvol_update_live_volsize(zv, volsize);
390 out:
391 if (owned) {
392 dmu_objset_disown(os, FTAG);
393 if (zv != NULL)
394 zv->zv_objset = NULL;
395 }
396 mutex_exit(&zvol_state_lock);
397 return (error);
398 }
399
400 /*
401 * Sanity check volume block size.
402 */
403 int
404 zvol_check_volblocksize(const char *name, uint64_t volblocksize)
405 {
406 /* Record sizes above 128k need the feature to be enabled */
407 if (volblocksize > SPA_OLD_MAXBLOCKSIZE) {
408 spa_t *spa;
409 int error;
410
411 if ((error = spa_open(name, &spa, FTAG)) != 0)
412 return (error);
413
414 if (!spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) {
415 spa_close(spa, FTAG);
416 return (SET_ERROR(ENOTSUP));
417 }
418
419 /*
420 * We don't allow setting the property above 1MB,
421 * unless the tunable has been changed.
422 */
423 if (volblocksize > zfs_max_recordsize)
424 return (SET_ERROR(EDOM));
425
426 spa_close(spa, FTAG);
427 }
428
429 if (volblocksize < SPA_MINBLOCKSIZE ||
430 volblocksize > SPA_MAXBLOCKSIZE ||
431 !ISP2(volblocksize))
432 return (SET_ERROR(EDOM));
433
434 return (0);
435 }
436
437 /*
438 * Set ZFS_PROP_VOLBLOCKSIZE set entry point.
439 */
440 int
441 zvol_set_volblocksize(const char *name, uint64_t volblocksize)
442 {
443 zvol_state_t *zv;
444 dmu_tx_t *tx;
445 int error;
446
447 mutex_enter(&zvol_state_lock);
448
449 zv = zvol_find_by_name(name);
450 if (zv == NULL) {
451 error = SET_ERROR(ENXIO);
452 goto out;
453 }
454
455 if (zv->zv_flags & ZVOL_RDONLY) {
456 error = SET_ERROR(EROFS);
457 goto out;
458 }
459
460 tx = dmu_tx_create(zv->zv_objset);
461 dmu_tx_hold_bonus(tx, ZVOL_OBJ);
462 error = dmu_tx_assign(tx, TXG_WAIT);
463 if (error) {
464 dmu_tx_abort(tx);
465 } else {
466 error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ,
467 volblocksize, 0, tx);
468 if (error == ENOTSUP)
469 error = SET_ERROR(EBUSY);
470 dmu_tx_commit(tx);
471 if (error == 0)
472 zv->zv_volblocksize = volblocksize;
473 }
474 out:
475 mutex_exit(&zvol_state_lock);
476
477 return (SET_ERROR(error));
478 }
479
480 /*
481 * Replay a TX_TRUNCATE ZIL transaction if asked. TX_TRUNCATE is how we
482 * implement DKIOCFREE/free-long-range.
483 */
484 static int
485 zvol_replay_truncate(zvol_state_t *zv, lr_truncate_t *lr, boolean_t byteswap)
486 {
487 uint64_t offset, length;
488
489 if (byteswap)
490 byteswap_uint64_array(lr, sizeof (*lr));
491
492 offset = lr->lr_offset;
493 length = lr->lr_length;
494
495 return (dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, offset, length));
496 }
497
498 /*
499 * Replay a TX_WRITE ZIL transaction that didn't get committed
500 * after a system failure
501 */
502 static int
503 zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
504 {
505 objset_t *os = zv->zv_objset;
506 char *data = (char *)(lr + 1); /* data follows lr_write_t */
507 uint64_t off = lr->lr_offset;
508 uint64_t len = lr->lr_length;
509 dmu_tx_t *tx;
510 int error;
511
512 if (byteswap)
513 byteswap_uint64_array(lr, sizeof (*lr));
514
515 tx = dmu_tx_create(os);
516 dmu_tx_hold_write(tx, ZVOL_OBJ, off, len);
517 error = dmu_tx_assign(tx, TXG_WAIT);
518 if (error) {
519 dmu_tx_abort(tx);
520 } else {
521 dmu_write(os, ZVOL_OBJ, off, len, data, tx);
522 dmu_tx_commit(tx);
523 }
524
525 return (SET_ERROR(error));
526 }
527
528 static int
529 zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
530 {
531 return (SET_ERROR(ENOTSUP));
532 }
533
534 /*
535 * Callback vectors for replaying records.
536 * Only TX_WRITE and TX_TRUNCATE are needed for zvol.
537 */
538 zil_replay_func_t zvol_replay_vector[TX_MAX_TYPE] = {
539 (zil_replay_func_t)zvol_replay_err, /* no such transaction type */
540 (zil_replay_func_t)zvol_replay_err, /* TX_CREATE */
541 (zil_replay_func_t)zvol_replay_err, /* TX_MKDIR */
542 (zil_replay_func_t)zvol_replay_err, /* TX_MKXATTR */
543 (zil_replay_func_t)zvol_replay_err, /* TX_SYMLINK */
544 (zil_replay_func_t)zvol_replay_err, /* TX_REMOVE */
545 (zil_replay_func_t)zvol_replay_err, /* TX_RMDIR */
546 (zil_replay_func_t)zvol_replay_err, /* TX_LINK */
547 (zil_replay_func_t)zvol_replay_err, /* TX_RENAME */
548 (zil_replay_func_t)zvol_replay_write, /* TX_WRITE */
549 (zil_replay_func_t)zvol_replay_truncate, /* TX_TRUNCATE */
550 (zil_replay_func_t)zvol_replay_err, /* TX_SETATTR */
551 (zil_replay_func_t)zvol_replay_err, /* TX_ACL */
552 };
553
554 /*
555 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
556 *
557 * We store data in the log buffers if it's small enough.
558 * Otherwise we will later flush the data out via dmu_sync().
559 */
560 ssize_t zvol_immediate_write_sz = 32768;
561
562 static void
563 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, uint64_t offset,
564 uint64_t size, int sync)
565 {
566 uint32_t blocksize = zv->zv_volblocksize;
567 zilog_t *zilog = zv->zv_zilog;
568 boolean_t slogging;
569 ssize_t immediate_write_sz;
570
571 if (zil_replaying(zilog, tx))
572 return;
573
574 immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
575 ? 0 : zvol_immediate_write_sz;
576 slogging = spa_has_slogs(zilog->zl_spa) &&
577 (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
578
579 while (size) {
580 itx_t *itx;
581 lr_write_t *lr;
582 ssize_t len;
583 itx_wr_state_t write_state;
584
585 /*
586 * Unlike zfs_log_write() we can be called with
587 * up to DMU_MAX_ACCESS/2 (5MB) writes.
588 */
589 if (blocksize > immediate_write_sz && !slogging &&
590 size >= blocksize && offset % blocksize == 0) {
591 write_state = WR_INDIRECT; /* uses dmu_sync */
592 len = blocksize;
593 } else if (sync) {
594 write_state = WR_COPIED;
595 len = MIN(ZIL_MAX_LOG_DATA, size);
596 } else {
597 write_state = WR_NEED_COPY;
598 len = MIN(ZIL_MAX_LOG_DATA, size);
599 }
600
601 itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
602 (write_state == WR_COPIED ? len : 0));
603 lr = (lr_write_t *)&itx->itx_lr;
604 if (write_state == WR_COPIED && dmu_read(zv->zv_objset,
605 ZVOL_OBJ, offset, len, lr+1, DMU_READ_NO_PREFETCH) != 0) {
606 zil_itx_destroy(itx);
607 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
608 lr = (lr_write_t *)&itx->itx_lr;
609 write_state = WR_NEED_COPY;
610 }
611
612 itx->itx_wr_state = write_state;
613 if (write_state == WR_NEED_COPY)
614 itx->itx_sod += len;
615 lr->lr_foid = ZVOL_OBJ;
616 lr->lr_offset = offset;
617 lr->lr_length = len;
618 lr->lr_blkoff = 0;
619 BP_ZERO(&lr->lr_blkptr);
620
621 itx->itx_private = zv;
622 itx->itx_sync = sync;
623
624 (void) zil_itx_assign(zilog, itx, tx);
625
626 offset += len;
627 size -= len;
628 }
629 }
630
631 static int
632 zvol_write(zvol_state_t *zv, uio_t *uio, boolean_t sync)
633 {
634 uint64_t volsize = zv->zv_volsize;
635 rl_t *rl;
636 int error = 0;
637
638 ASSERT(zv && zv->zv_open_count > 0);
639
640 rl = zfs_range_lock(&zv->zv_range_lock, uio->uio_loffset,
641 uio->uio_resid, RL_WRITER);
642
643 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
644 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
645 uint64_t off = uio->uio_loffset;
646 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
647
648 if (bytes > volsize - off) /* don't write past the end */
649 bytes = volsize - off;
650
651 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
652
653 /* This will only fail for ENOSPC */
654 error = dmu_tx_assign(tx, TXG_WAIT);
655 if (error) {
656 dmu_tx_abort(tx);
657 break;
658 }
659 error = dmu_write_uio_dbuf(zv->zv_dbuf, uio, bytes, tx);
660 if (error == 0)
661 zvol_log_write(zv, tx, off, bytes, sync);
662 dmu_tx_commit(tx);
663
664 if (error)
665 break;
666 }
667 zfs_range_unlock(rl);
668 if (sync)
669 zil_commit(zv->zv_zilog, ZVOL_OBJ);
670 return (error);
671 }
672
673 /*
674 * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE.
675 */
676 static void
677 zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off, uint64_t len,
678 boolean_t sync)
679 {
680 itx_t *itx;
681 lr_truncate_t *lr;
682 zilog_t *zilog = zv->zv_zilog;
683
684 if (zil_replaying(zilog, tx))
685 return;
686
687 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
688 lr = (lr_truncate_t *)&itx->itx_lr;
689 lr->lr_foid = ZVOL_OBJ;
690 lr->lr_offset = off;
691 lr->lr_length = len;
692
693 itx->itx_sync = sync;
694 zil_itx_assign(zilog, itx, tx);
695 }
696
697 static int
698 zvol_discard(struct bio *bio)
699 {
700 zvol_state_t *zv = bio->bi_bdev->bd_disk->private_data;
701 uint64_t start = BIO_BI_SECTOR(bio) << 9;
702 uint64_t size = BIO_BI_SIZE(bio);
703 uint64_t end = start + size;
704 int error;
705 rl_t *rl;
706 dmu_tx_t *tx;
707
708 ASSERT(zv && zv->zv_open_count > 0);
709
710 if (end > zv->zv_volsize)
711 return (SET_ERROR(EIO));
712
713 /*
714 * Align the request to volume block boundaries when a secure erase is
715 * not required. This will prevent dnode_free_range() from zeroing out
716 * the unaligned parts which is slow (read-modify-write) and useless
717 * since we are not freeing any space by doing so.
718 */
719 if (!bio_is_secure_erase(bio)) {
720 start = P2ROUNDUP(start, zv->zv_volblocksize);
721 end = P2ALIGN(end, zv->zv_volblocksize);
722 size = end - start;
723 }
724
725 if (start >= end)
726 return (0);
727
728 rl = zfs_range_lock(&zv->zv_range_lock, start, size, RL_WRITER);
729 tx = dmu_tx_create(zv->zv_objset);
730 dmu_tx_mark_netfree(tx);
731 error = dmu_tx_assign(tx, TXG_WAIT);
732 if (error != 0) {
733 dmu_tx_abort(tx);
734 } else {
735 zvol_log_truncate(zv, tx, start, size, B_TRUE);
736 dmu_tx_commit(tx);
737 error = dmu_free_long_range(zv->zv_objset,
738 ZVOL_OBJ, start, size);
739 }
740
741 zfs_range_unlock(rl);
742
743 return (error);
744 }
745
746 static int
747 zvol_read(zvol_state_t *zv, uio_t *uio)
748 {
749 uint64_t volsize = zv->zv_volsize;
750 rl_t *rl;
751 int error = 0;
752
753 ASSERT(zv && zv->zv_open_count > 0);
754
755 rl = zfs_range_lock(&zv->zv_range_lock, uio->uio_loffset,
756 uio->uio_resid, RL_READER);
757 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
758 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
759
760 /* don't read past the end */
761 if (bytes > volsize - uio->uio_loffset)
762 bytes = volsize - uio->uio_loffset;
763
764 error = dmu_read_uio_dbuf(zv->zv_dbuf, uio, bytes);
765 if (error) {
766 /* convert checksum errors into IO errors */
767 if (error == ECKSUM)
768 error = SET_ERROR(EIO);
769 break;
770 }
771 }
772 zfs_range_unlock(rl);
773 return (error);
774 }
775
776 static MAKE_REQUEST_FN_RET
777 zvol_request(struct request_queue *q, struct bio *bio)
778 {
779 uio_t uio;
780 zvol_state_t *zv = q->queuedata;
781 fstrans_cookie_t cookie = spl_fstrans_mark();
782 int rw = bio_data_dir(bio);
783 #ifdef HAVE_GENERIC_IO_ACCT
784 unsigned long start = jiffies;
785 #endif
786 int error = 0;
787
788 uio.uio_bvec = &bio->bi_io_vec[BIO_BI_IDX(bio)];
789 uio.uio_skip = BIO_BI_SKIP(bio);
790 uio.uio_resid = BIO_BI_SIZE(bio);
791 uio.uio_iovcnt = bio->bi_vcnt - BIO_BI_IDX(bio);
792 uio.uio_loffset = BIO_BI_SECTOR(bio) << 9;
793 uio.uio_limit = MAXOFFSET_T;
794 uio.uio_segflg = UIO_BVEC;
795
796 if (bio_has_data(bio) && uio.uio_loffset + uio.uio_resid >
797 zv->zv_volsize) {
798 printk(KERN_INFO
799 "%s: bad access: offset=%llu, size=%lu\n",
800 zv->zv_disk->disk_name,
801 (long long unsigned)uio.uio_loffset,
802 (long unsigned)uio.uio_resid);
803 error = SET_ERROR(EIO);
804 goto out1;
805 }
806
807 generic_start_io_acct(rw, bio_sectors(bio), &zv->zv_disk->part0);
808
809 if (rw == WRITE) {
810 if (unlikely(zv->zv_flags & ZVOL_RDONLY)) {
811 error = SET_ERROR(EROFS);
812 goto out2;
813 }
814
815 if (bio_is_discard(bio) || bio_is_secure_erase(bio)) {
816 error = zvol_discard(bio);
817 goto out2;
818 }
819
820 /*
821 * Some requests are just for flush and nothing else.
822 */
823 if (uio.uio_resid == 0) {
824 if (bio_is_flush(bio))
825 zil_commit(zv->zv_zilog, ZVOL_OBJ);
826 goto out2;
827 }
828
829 error = zvol_write(zv, &uio,
830 bio_is_flush(bio) || bio_is_fua(bio) ||
831 zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS);
832 } else
833 error = zvol_read(zv, &uio);
834
835 out2:
836 generic_end_io_acct(rw, &zv->zv_disk->part0, start);
837 out1:
838 BIO_END_IO(bio, -error);
839 spl_fstrans_unmark(cookie);
840 #ifdef HAVE_MAKE_REQUEST_FN_RET_INT
841 return (0);
842 #elif defined(HAVE_MAKE_REQUEST_FN_RET_QC)
843 return (BLK_QC_T_NONE);
844 #endif
845 }
846
847 static void
848 zvol_get_done(zgd_t *zgd, int error)
849 {
850 if (zgd->zgd_db)
851 dmu_buf_rele(zgd->zgd_db, zgd);
852
853 zfs_range_unlock(zgd->zgd_rl);
854
855 if (error == 0 && zgd->zgd_bp)
856 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
857
858 kmem_free(zgd, sizeof (zgd_t));
859 }
860
861 /*
862 * Get data to generate a TX_WRITE intent log record.
863 */
864 static int
865 zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
866 {
867 zvol_state_t *zv = arg;
868 objset_t *os = zv->zv_objset;
869 uint64_t object = ZVOL_OBJ;
870 uint64_t offset = lr->lr_offset;
871 uint64_t size = lr->lr_length;
872 blkptr_t *bp = &lr->lr_blkptr;
873 dmu_buf_t *db;
874 zgd_t *zgd;
875 int error;
876
877 ASSERT(zio != NULL);
878 ASSERT(size != 0);
879
880 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
881 zgd->zgd_zilog = zv->zv_zilog;
882 zgd->zgd_rl = zfs_range_lock(&zv->zv_range_lock, offset, size,
883 RL_READER);
884
885 /*
886 * Write records come in two flavors: immediate and indirect.
887 * For small writes it's cheaper to store the data with the
888 * log record (immediate); for large writes it's cheaper to
889 * sync the data and get a pointer to it (indirect) so that
890 * we don't have to write the data twice.
891 */
892 if (buf != NULL) { /* immediate write */
893 error = dmu_read(os, object, offset, size, buf,
894 DMU_READ_NO_PREFETCH);
895 } else {
896 size = zv->zv_volblocksize;
897 offset = P2ALIGN_TYPED(offset, size, uint64_t);
898 error = dmu_buf_hold(os, object, offset, zgd, &db,
899 DMU_READ_NO_PREFETCH);
900 if (error == 0) {
901 blkptr_t *obp = dmu_buf_get_blkptr(db);
902 if (obp) {
903 ASSERT(BP_IS_HOLE(bp));
904 *bp = *obp;
905 }
906
907 zgd->zgd_db = db;
908 zgd->zgd_bp = &lr->lr_blkptr;
909
910 ASSERT(db != NULL);
911 ASSERT(db->db_offset == offset);
912 ASSERT(db->db_size == size);
913
914 error = dmu_sync(zio, lr->lr_common.lrc_txg,
915 zvol_get_done, zgd);
916
917 if (error == 0)
918 return (0);
919 }
920 }
921
922 zvol_get_done(zgd, error);
923
924 return (SET_ERROR(error));
925 }
926
927 /*
928 * The zvol_state_t's are inserted into zvol_state_list and zvol_htable.
929 */
930 static void
931 zvol_insert(zvol_state_t *zv)
932 {
933 ASSERT(MUTEX_HELD(&zvol_state_lock));
934 ASSERT3U(MINOR(zv->zv_dev) & ZVOL_MINOR_MASK, ==, 0);
935 list_insert_head(&zvol_state_list, zv);
936 hlist_add_head(&zv->zv_hlink, ZVOL_HT_HEAD(zv->zv_hash));
937 }
938
939 /*
940 * Simply remove the zvol from to list of zvols.
941 */
942 static void
943 zvol_remove(zvol_state_t *zv)
944 {
945 ASSERT(MUTEX_HELD(&zvol_state_lock));
946 list_remove(&zvol_state_list, zv);
947 hlist_del(&zv->zv_hlink);
948 }
949
950 static int
951 zvol_first_open(zvol_state_t *zv)
952 {
953 objset_t *os;
954 uint64_t volsize;
955 int error;
956 uint64_t ro;
957
958 /* lie and say we're read-only */
959 error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, 1, zvol_tag, &os);
960 if (error)
961 return (SET_ERROR(-error));
962
963 zv->zv_objset = os;
964
965 error = dsl_prop_get_integer(zv->zv_name, "readonly", &ro, NULL);
966 if (error)
967 goto out_owned;
968
969 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
970 if (error)
971 goto out_owned;
972
973 error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf);
974 if (error)
975 goto out_owned;
976
977 set_capacity(zv->zv_disk, volsize >> 9);
978 zv->zv_volsize = volsize;
979 zv->zv_zilog = zil_open(os, zvol_get_data);
980
981 if (ro || dmu_objset_is_snapshot(os) ||
982 !spa_writeable(dmu_objset_spa(os))) {
983 set_disk_ro(zv->zv_disk, 1);
984 zv->zv_flags |= ZVOL_RDONLY;
985 } else {
986 set_disk_ro(zv->zv_disk, 0);
987 zv->zv_flags &= ~ZVOL_RDONLY;
988 }
989
990 out_owned:
991 if (error) {
992 dmu_objset_disown(os, zvol_tag);
993 zv->zv_objset = NULL;
994 }
995
996 return (SET_ERROR(-error));
997 }
998
999 static void
1000 zvol_last_close(zvol_state_t *zv)
1001 {
1002 zil_close(zv->zv_zilog);
1003 zv->zv_zilog = NULL;
1004
1005 dmu_buf_rele(zv->zv_dbuf, zvol_tag);
1006 zv->zv_dbuf = NULL;
1007
1008 /*
1009 * Evict cached data
1010 */
1011 if (dsl_dataset_is_dirty(dmu_objset_ds(zv->zv_objset)) &&
1012 !(zv->zv_flags & ZVOL_RDONLY))
1013 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
1014 (void) dmu_objset_evict_dbufs(zv->zv_objset);
1015
1016 dmu_objset_disown(zv->zv_objset, zvol_tag);
1017 zv->zv_objset = NULL;
1018 }
1019
1020 static int
1021 zvol_open(struct block_device *bdev, fmode_t flag)
1022 {
1023 zvol_state_t *zv;
1024 int error = 0, drop_mutex = 0;
1025
1026 /*
1027 * If the caller is already holding the mutex do not take it
1028 * again, this will happen as part of zvol_create_minor_impl().
1029 * Once add_disk() is called the device is live and the kernel
1030 * will attempt to open it to read the partition information.
1031 */
1032 if (!mutex_owned(&zvol_state_lock)) {
1033 mutex_enter(&zvol_state_lock);
1034 drop_mutex = 1;
1035 }
1036
1037 /*
1038 * Obtain a copy of private_data under the lock to make sure
1039 * that either the result of zvol_freeg() setting
1040 * bdev->bd_disk->private_data to NULL is observed, or zvol_free()
1041 * is not called on this zv because of the positive zv_open_count.
1042 */
1043 zv = bdev->bd_disk->private_data;
1044 if (zv == NULL) {
1045 error = -ENXIO;
1046 goto out_mutex;
1047 }
1048
1049 if (zv->zv_open_count == 0) {
1050 error = zvol_first_open(zv);
1051 if (error)
1052 goto out_mutex;
1053 }
1054
1055 if ((flag & FMODE_WRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
1056 error = -EROFS;
1057 goto out_open_count;
1058 }
1059
1060 zv->zv_open_count++;
1061
1062 check_disk_change(bdev);
1063
1064 out_open_count:
1065 if (zv->zv_open_count == 0)
1066 zvol_last_close(zv);
1067
1068 out_mutex:
1069 if (drop_mutex)
1070 mutex_exit(&zvol_state_lock);
1071
1072 return (SET_ERROR(error));
1073 }
1074
1075 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
1076 static void
1077 #else
1078 static int
1079 #endif
1080 zvol_release(struct gendisk *disk, fmode_t mode)
1081 {
1082 zvol_state_t *zv = disk->private_data;
1083 int drop_mutex = 0;
1084
1085 ASSERT(zv && zv->zv_open_count > 0);
1086
1087 if (!mutex_owned(&zvol_state_lock)) {
1088 mutex_enter(&zvol_state_lock);
1089 drop_mutex = 1;
1090 }
1091
1092 zv->zv_open_count--;
1093 if (zv->zv_open_count == 0)
1094 zvol_last_close(zv);
1095
1096 if (drop_mutex)
1097 mutex_exit(&zvol_state_lock);
1098
1099 #ifndef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
1100 return (0);
1101 #endif
1102 }
1103
1104 static int
1105 zvol_ioctl(struct block_device *bdev, fmode_t mode,
1106 unsigned int cmd, unsigned long arg)
1107 {
1108 zvol_state_t *zv = bdev->bd_disk->private_data;
1109 int error = 0;
1110
1111 ASSERT(zv && zv->zv_open_count > 0);
1112
1113 switch (cmd) {
1114 case BLKFLSBUF:
1115 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1116 break;
1117 case BLKZNAME:
1118 error = copy_to_user((void *)arg, zv->zv_name, MAXNAMELEN);
1119 break;
1120
1121 default:
1122 error = -ENOTTY;
1123 break;
1124
1125 }
1126
1127 return (SET_ERROR(error));
1128 }
1129
1130 #ifdef CONFIG_COMPAT
1131 static int
1132 zvol_compat_ioctl(struct block_device *bdev, fmode_t mode,
1133 unsigned cmd, unsigned long arg)
1134 {
1135 return (zvol_ioctl(bdev, mode, cmd, arg));
1136 }
1137 #else
1138 #define zvol_compat_ioctl NULL
1139 #endif
1140
1141 static int zvol_media_changed(struct gendisk *disk)
1142 {
1143 zvol_state_t *zv = disk->private_data;
1144
1145 ASSERT(zv && zv->zv_open_count > 0);
1146
1147 return (zv->zv_changed);
1148 }
1149
1150 static int zvol_revalidate_disk(struct gendisk *disk)
1151 {
1152 zvol_state_t *zv = disk->private_data;
1153
1154 ASSERT(zv && zv->zv_open_count > 0);
1155
1156 zv->zv_changed = 0;
1157 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1158
1159 return (0);
1160 }
1161
1162 /*
1163 * Provide a simple virtual geometry for legacy compatibility. For devices
1164 * smaller than 1 MiB a small head and sector count is used to allow very
1165 * tiny devices. For devices over 1 Mib a standard head and sector count
1166 * is used to keep the cylinders count reasonable.
1167 */
1168 static int
1169 zvol_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1170 {
1171 zvol_state_t *zv = bdev->bd_disk->private_data;
1172 sector_t sectors;
1173
1174 ASSERT(zv && zv->zv_open_count > 0);
1175
1176 sectors = get_capacity(zv->zv_disk);
1177
1178 if (sectors > 2048) {
1179 geo->heads = 16;
1180 geo->sectors = 63;
1181 } else {
1182 geo->heads = 2;
1183 geo->sectors = 4;
1184 }
1185
1186 geo->start = 0;
1187 geo->cylinders = sectors / (geo->heads * geo->sectors);
1188
1189 return (0);
1190 }
1191
1192 static struct kobject *
1193 zvol_probe(dev_t dev, int *part, void *arg)
1194 {
1195 zvol_state_t *zv;
1196 struct kobject *kobj;
1197
1198 mutex_enter(&zvol_state_lock);
1199 zv = zvol_find_by_dev(dev);
1200 kobj = zv ? get_disk(zv->zv_disk) : NULL;
1201 mutex_exit(&zvol_state_lock);
1202
1203 return (kobj);
1204 }
1205
1206 #ifdef HAVE_BDEV_BLOCK_DEVICE_OPERATIONS
1207 static struct block_device_operations zvol_ops = {
1208 .open = zvol_open,
1209 .release = zvol_release,
1210 .ioctl = zvol_ioctl,
1211 .compat_ioctl = zvol_compat_ioctl,
1212 .media_changed = zvol_media_changed,
1213 .revalidate_disk = zvol_revalidate_disk,
1214 .getgeo = zvol_getgeo,
1215 .owner = THIS_MODULE,
1216 };
1217
1218 #else /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1219
1220 static int
1221 zvol_open_by_inode(struct inode *inode, struct file *file)
1222 {
1223 return (zvol_open(inode->i_bdev, file->f_mode));
1224 }
1225
1226 static int
1227 zvol_release_by_inode(struct inode *inode, struct file *file)
1228 {
1229 return (zvol_release(inode->i_bdev->bd_disk, file->f_mode));
1230 }
1231
1232 static int
1233 zvol_ioctl_by_inode(struct inode *inode, struct file *file,
1234 unsigned int cmd, unsigned long arg)
1235 {
1236 if (file == NULL || inode == NULL)
1237 return (SET_ERROR(-EINVAL));
1238
1239 return (zvol_ioctl(inode->i_bdev, file->f_mode, cmd, arg));
1240 }
1241
1242 #ifdef CONFIG_COMPAT
1243 static long
1244 zvol_compat_ioctl_by_inode(struct file *file,
1245 unsigned int cmd, unsigned long arg)
1246 {
1247 if (file == NULL)
1248 return (SET_ERROR(-EINVAL));
1249
1250 return (zvol_compat_ioctl(file->f_dentry->d_inode->i_bdev,
1251 file->f_mode, cmd, arg));
1252 }
1253 #else
1254 #define zvol_compat_ioctl_by_inode NULL
1255 #endif
1256
1257 static struct block_device_operations zvol_ops = {
1258 .open = zvol_open_by_inode,
1259 .release = zvol_release_by_inode,
1260 .ioctl = zvol_ioctl_by_inode,
1261 .compat_ioctl = zvol_compat_ioctl_by_inode,
1262 .media_changed = zvol_media_changed,
1263 .revalidate_disk = zvol_revalidate_disk,
1264 .getgeo = zvol_getgeo,
1265 .owner = THIS_MODULE,
1266 };
1267 #endif /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1268
1269 /*
1270 * Allocate memory for a new zvol_state_t and setup the required
1271 * request queue and generic disk structures for the block device.
1272 */
1273 static zvol_state_t *
1274 zvol_alloc(dev_t dev, const char *name)
1275 {
1276 zvol_state_t *zv;
1277
1278 zv = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
1279
1280 list_link_init(&zv->zv_next);
1281
1282 zv->zv_queue = blk_alloc_queue(GFP_ATOMIC);
1283 if (zv->zv_queue == NULL)
1284 goto out_kmem;
1285
1286 blk_queue_make_request(zv->zv_queue, zvol_request);
1287 blk_queue_set_write_cache(zv->zv_queue, B_TRUE, B_TRUE);
1288
1289 zv->zv_disk = alloc_disk(ZVOL_MINORS);
1290 if (zv->zv_disk == NULL)
1291 goto out_queue;
1292
1293 zv->zv_queue->queuedata = zv;
1294 zv->zv_dev = dev;
1295 zv->zv_open_count = 0;
1296 strlcpy(zv->zv_name, name, MAXNAMELEN);
1297
1298 zfs_rlock_init(&zv->zv_range_lock);
1299
1300 zv->zv_disk->major = zvol_major;
1301 zv->zv_disk->first_minor = (dev & MINORMASK);
1302 zv->zv_disk->fops = &zvol_ops;
1303 zv->zv_disk->private_data = zv;
1304 zv->zv_disk->queue = zv->zv_queue;
1305 snprintf(zv->zv_disk->disk_name, DISK_NAME_LEN, "%s%d",
1306 ZVOL_DEV_NAME, (dev & MINORMASK));
1307
1308 return (zv);
1309
1310 out_queue:
1311 blk_cleanup_queue(zv->zv_queue);
1312 out_kmem:
1313 kmem_free(zv, sizeof (zvol_state_t));
1314
1315 return (NULL);
1316 }
1317
1318 /*
1319 * Cleanup then free a zvol_state_t which was created by zvol_alloc().
1320 */
1321 static void
1322 zvol_free(zvol_state_t *zv)
1323 {
1324 ASSERT(MUTEX_HELD(&zvol_state_lock));
1325 ASSERT(zv->zv_open_count == 0);
1326
1327 zfs_rlock_destroy(&zv->zv_range_lock);
1328
1329 zv->zv_disk->private_data = NULL;
1330
1331 del_gendisk(zv->zv_disk);
1332 blk_cleanup_queue(zv->zv_queue);
1333 put_disk(zv->zv_disk);
1334
1335 ida_simple_remove(&zvol_ida, MINOR(zv->zv_dev) >> ZVOL_MINOR_BITS);
1336 kmem_free(zv, sizeof (zvol_state_t));
1337 }
1338
1339 /*
1340 * Create a block device minor node and setup the linkage between it
1341 * and the specified volume. Once this function returns the block
1342 * device is live and ready for use.
1343 */
1344 static int
1345 zvol_create_minor_impl(const char *name)
1346 {
1347 zvol_state_t *zv;
1348 objset_t *os;
1349 dmu_object_info_t *doi;
1350 uint64_t volsize;
1351 uint64_t len;
1352 unsigned minor = 0;
1353 int error = 0;
1354 int idx;
1355 uint64_t hash = zvol_name_hash(name);
1356
1357 idx = ida_simple_get(&zvol_ida, 0, 0, kmem_flags_convert(KM_SLEEP));
1358 if (idx < 0)
1359 return (SET_ERROR(-idx));
1360 minor = idx << ZVOL_MINOR_BITS;
1361
1362 mutex_enter(&zvol_state_lock);
1363
1364 zv = zvol_find_by_name_hash(name, hash);
1365 if (zv) {
1366 error = SET_ERROR(EEXIST);
1367 goto out;
1368 }
1369
1370 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
1371
1372 error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os);
1373 if (error)
1374 goto out_doi;
1375
1376 error = dmu_object_info(os, ZVOL_OBJ, doi);
1377 if (error)
1378 goto out_dmu_objset_disown;
1379
1380 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
1381 if (error)
1382 goto out_dmu_objset_disown;
1383
1384 zv = zvol_alloc(MKDEV(zvol_major, minor), name);
1385 if (zv == NULL) {
1386 error = SET_ERROR(EAGAIN);
1387 goto out_dmu_objset_disown;
1388 }
1389 zv->zv_hash = hash;
1390
1391 if (dmu_objset_is_snapshot(os))
1392 zv->zv_flags |= ZVOL_RDONLY;
1393
1394 zv->zv_volblocksize = doi->doi_data_block_size;
1395 zv->zv_volsize = volsize;
1396 zv->zv_objset = os;
1397
1398 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1399
1400 blk_queue_max_hw_sectors(zv->zv_queue, (DMU_MAX_ACCESS / 4) >> 9);
1401 blk_queue_max_segments(zv->zv_queue, UINT16_MAX);
1402 blk_queue_max_segment_size(zv->zv_queue, UINT_MAX);
1403 blk_queue_physical_block_size(zv->zv_queue, zv->zv_volblocksize);
1404 blk_queue_io_opt(zv->zv_queue, zv->zv_volblocksize);
1405 blk_queue_max_discard_sectors(zv->zv_queue,
1406 (zvol_max_discard_blocks * zv->zv_volblocksize) >> 9);
1407 blk_queue_discard_granularity(zv->zv_queue, zv->zv_volblocksize);
1408 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, zv->zv_queue);
1409 #ifdef QUEUE_FLAG_NONROT
1410 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zv->zv_queue);
1411 #endif
1412 #ifdef QUEUE_FLAG_ADD_RANDOM
1413 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, zv->zv_queue);
1414 #endif
1415
1416 if (spa_writeable(dmu_objset_spa(os))) {
1417 if (zil_replay_disable)
1418 zil_destroy(dmu_objset_zil(os), B_FALSE);
1419 else
1420 zil_replay(os, zv, zvol_replay_vector);
1421 }
1422
1423 /*
1424 * When udev detects the addition of the device it will immediately
1425 * invoke blkid(8) to determine the type of content on the device.
1426 * Prefetching the blocks commonly scanned by blkid(8) will speed
1427 * up this process.
1428 */
1429 len = MIN(MAX(zvol_prefetch_bytes, 0), SPA_MAXBLOCKSIZE);
1430 if (len > 0) {
1431 dmu_prefetch(os, ZVOL_OBJ, 0, 0, len, ZIO_PRIORITY_SYNC_READ);
1432 dmu_prefetch(os, ZVOL_OBJ, 0, volsize - len, len,
1433 ZIO_PRIORITY_SYNC_READ);
1434 }
1435
1436 zv->zv_objset = NULL;
1437 out_dmu_objset_disown:
1438 dmu_objset_disown(os, zvol_tag);
1439 out_doi:
1440 kmem_free(doi, sizeof (dmu_object_info_t));
1441 out:
1442
1443 if (error == 0) {
1444 zvol_insert(zv);
1445 /*
1446 * Drop the lock to prevent deadlock with sys_open() ->
1447 * zvol_open(), which first takes bd_disk->bd_mutex and then
1448 * takes zvol_state_lock, whereas this code path first takes
1449 * zvol_state_lock, and then takes bd_disk->bd_mutex.
1450 */
1451 mutex_exit(&zvol_state_lock);
1452 add_disk(zv->zv_disk);
1453 } else {
1454 mutex_exit(&zvol_state_lock);
1455 ida_simple_remove(&zvol_ida, idx);
1456 }
1457
1458 return (SET_ERROR(error));
1459 }
1460
1461 /*
1462 * Rename a block device minor mode for the specified volume.
1463 */
1464 static void
1465 zvol_rename_minor(zvol_state_t *zv, const char *newname)
1466 {
1467 int readonly = get_disk_ro(zv->zv_disk);
1468
1469 ASSERT(MUTEX_HELD(&zvol_state_lock));
1470
1471 strlcpy(zv->zv_name, newname, sizeof (zv->zv_name));
1472
1473 /*
1474 * The block device's read-only state is briefly changed causing
1475 * a KOBJ_CHANGE uevent to be issued. This ensures udev detects
1476 * the name change and fixes the symlinks. This does not change
1477 * ZVOL_RDONLY in zv->zv_flags so the actual read-only state never
1478 * changes. This would normally be done using kobject_uevent() but
1479 * that is a GPL-only symbol which is why we need this workaround.
1480 */
1481 set_disk_ro(zv->zv_disk, !readonly);
1482 set_disk_ro(zv->zv_disk, readonly);
1483 }
1484
1485 typedef struct minors_job {
1486 list_t *list;
1487 list_node_t link;
1488 /* input */
1489 char *name;
1490 /* output */
1491 int error;
1492 } minors_job_t;
1493
1494 /*
1495 * Prefetch zvol dnodes for the minors_job
1496 */
1497 static void
1498 zvol_prefetch_minors_impl(void *arg)
1499 {
1500 minors_job_t *job = arg;
1501 char *dsname = job->name;
1502 objset_t *os = NULL;
1503
1504 job->error = dmu_objset_own(dsname, DMU_OST_ZVOL, B_TRUE, zvol_tag,
1505 &os);
1506 if (job->error == 0) {
1507 dmu_prefetch(os, ZVOL_OBJ, 0, 0, 0, ZIO_PRIORITY_SYNC_READ);
1508 dmu_objset_disown(os, zvol_tag);
1509 }
1510 }
1511
1512 /*
1513 * Mask errors to continue dmu_objset_find() traversal
1514 */
1515 static int
1516 zvol_create_snap_minor_cb(const char *dsname, void *arg)
1517 {
1518 minors_job_t *j = arg;
1519 list_t *minors_list = j->list;
1520 const char *name = j->name;
1521
1522 ASSERT0(MUTEX_HELD(&spa_namespace_lock));
1523
1524 /* skip the designated dataset */
1525 if (name && strcmp(dsname, name) == 0)
1526 return (0);
1527
1528 /* at this point, the dsname should name a snapshot */
1529 if (strchr(dsname, '@') == 0) {
1530 dprintf("zvol_create_snap_minor_cb(): "
1531 "%s is not a shapshot name\n", dsname);
1532 } else {
1533 minors_job_t *job;
1534 char *n = strdup(dsname);
1535 if (n == NULL)
1536 return (0);
1537
1538 job = kmem_alloc(sizeof (minors_job_t), KM_SLEEP);
1539 job->name = n;
1540 job->list = minors_list;
1541 job->error = 0;
1542 list_insert_tail(minors_list, job);
1543 /* don't care if dispatch fails, because job->error is 0 */
1544 taskq_dispatch(system_taskq, zvol_prefetch_minors_impl, job,
1545 TQ_SLEEP);
1546 }
1547
1548 return (0);
1549 }
1550
1551 /*
1552 * Mask errors to continue dmu_objset_find() traversal
1553 */
1554 static int
1555 zvol_create_minors_cb(const char *dsname, void *arg)
1556 {
1557 uint64_t snapdev;
1558 int error;
1559 list_t *minors_list = arg;
1560
1561 ASSERT0(MUTEX_HELD(&spa_namespace_lock));
1562
1563 error = dsl_prop_get_integer(dsname, "snapdev", &snapdev, NULL);
1564 if (error)
1565 return (0);
1566
1567 /*
1568 * Given the name and the 'snapdev' property, create device minor nodes
1569 * with the linkages to zvols/snapshots as needed.
1570 * If the name represents a zvol, create a minor node for the zvol, then
1571 * check if its snapshots are 'visible', and if so, iterate over the
1572 * snapshots and create device minor nodes for those.
1573 */
1574 if (strchr(dsname, '@') == 0) {
1575 minors_job_t *job;
1576 char *n = strdup(dsname);
1577 if (n == NULL)
1578 return (0);
1579
1580 job = kmem_alloc(sizeof (minors_job_t), KM_SLEEP);
1581 job->name = n;
1582 job->list = minors_list;
1583 job->error = 0;
1584 list_insert_tail(minors_list, job);
1585 /* don't care if dispatch fails, because job->error is 0 */
1586 taskq_dispatch(system_taskq, zvol_prefetch_minors_impl, job,
1587 TQ_SLEEP);
1588
1589 if (snapdev == ZFS_SNAPDEV_VISIBLE) {
1590 /*
1591 * traverse snapshots only, do not traverse children,
1592 * and skip the 'dsname'
1593 */
1594 error = dmu_objset_find((char *)dsname,
1595 zvol_create_snap_minor_cb, (void *)job,
1596 DS_FIND_SNAPSHOTS);
1597 }
1598 } else {
1599 dprintf("zvol_create_minors_cb(): %s is not a zvol name\n",
1600 dsname);
1601 }
1602
1603 return (0);
1604 }
1605
1606 /*
1607 * Create minors for the specified dataset, including children and snapshots.
1608 * Pay attention to the 'snapdev' property and iterate over the snapshots
1609 * only if they are 'visible'. This approach allows one to assure that the
1610 * snapshot metadata is read from disk only if it is needed.
1611 *
1612 * The name can represent a dataset to be recursively scanned for zvols and
1613 * their snapshots, or a single zvol snapshot. If the name represents a
1614 * dataset, the scan is performed in two nested stages:
1615 * - scan the dataset for zvols, and
1616 * - for each zvol, create a minor node, then check if the zvol's snapshots
1617 * are 'visible', and only then iterate over the snapshots if needed
1618 *
1619 * If the name represents a snapshot, a check is perfromed if the snapshot is
1620 * 'visible' (which also verifies that the parent is a zvol), and if so,
1621 * a minor node for that snapshot is created.
1622 */
1623 static int
1624 zvol_create_minors_impl(const char *name)
1625 {
1626 int error = 0;
1627 fstrans_cookie_t cookie;
1628 char *atp, *parent;
1629 list_t minors_list;
1630 minors_job_t *job;
1631
1632 if (zvol_inhibit_dev)
1633 return (0);
1634
1635 /*
1636 * This is the list for prefetch jobs. Whenever we found a match
1637 * during dmu_objset_find, we insert a minors_job to the list and do
1638 * taskq_dispatch to parallel prefetch zvol dnodes. Note we don't need
1639 * any lock because all list operation is done on the current thread.
1640 *
1641 * We will use this list to do zvol_create_minor_impl after prefetch
1642 * so we don't have to traverse using dmu_objset_find again.
1643 */
1644 list_create(&minors_list, sizeof (minors_job_t),
1645 offsetof(minors_job_t, link));
1646
1647 parent = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1648 (void) strlcpy(parent, name, MAXPATHLEN);
1649
1650 if ((atp = strrchr(parent, '@')) != NULL) {
1651 uint64_t snapdev;
1652
1653 *atp = '\0';
1654 error = dsl_prop_get_integer(parent, "snapdev",
1655 &snapdev, NULL);
1656
1657 if (error == 0 && snapdev == ZFS_SNAPDEV_VISIBLE)
1658 error = zvol_create_minor_impl(name);
1659 } else {
1660 cookie = spl_fstrans_mark();
1661 error = dmu_objset_find(parent, zvol_create_minors_cb,
1662 &minors_list, DS_FIND_CHILDREN);
1663 spl_fstrans_unmark(cookie);
1664 }
1665
1666 kmem_free(parent, MAXPATHLEN);
1667 taskq_wait_outstanding(system_taskq, 0);
1668
1669 /*
1670 * Prefetch is completed, we can do zvol_create_minor_impl
1671 * sequentially.
1672 */
1673 while ((job = list_head(&minors_list)) != NULL) {
1674 list_remove(&minors_list, job);
1675 if (!job->error)
1676 zvol_create_minor_impl(job->name);
1677 strfree(job->name);
1678 kmem_free(job, sizeof (minors_job_t));
1679 }
1680
1681 list_destroy(&minors_list);
1682
1683 return (SET_ERROR(error));
1684 }
1685
1686 /*
1687 * Remove minors for specified dataset including children and snapshots.
1688 */
1689 static void
1690 zvol_remove_minors_impl(const char *name)
1691 {
1692 zvol_state_t *zv, *zv_next;
1693 int namelen = ((name) ? strlen(name) : 0);
1694
1695 if (zvol_inhibit_dev)
1696 return;
1697
1698 mutex_enter(&zvol_state_lock);
1699
1700 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1701 zv_next = list_next(&zvol_state_list, zv);
1702
1703 if (name == NULL || strcmp(zv->zv_name, name) == 0 ||
1704 (strncmp(zv->zv_name, name, namelen) == 0 &&
1705 (zv->zv_name[namelen] == '/' ||
1706 zv->zv_name[namelen] == '@'))) {
1707
1708 /* If in use, leave alone */
1709 if (zv->zv_open_count > 0)
1710 continue;
1711
1712 zvol_remove(zv);
1713 zvol_free(zv);
1714 }
1715 }
1716
1717 mutex_exit(&zvol_state_lock);
1718 }
1719
1720 /* Remove minor for this specific snapshot only */
1721 static void
1722 zvol_remove_minor_impl(const char *name)
1723 {
1724 zvol_state_t *zv, *zv_next;
1725
1726 if (zvol_inhibit_dev)
1727 return;
1728
1729 if (strchr(name, '@') == NULL)
1730 return;
1731
1732 mutex_enter(&zvol_state_lock);
1733
1734 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1735 zv_next = list_next(&zvol_state_list, zv);
1736
1737 if (strcmp(zv->zv_name, name) == 0) {
1738 /* If in use, leave alone */
1739 if (zv->zv_open_count > 0)
1740 continue;
1741 zvol_remove(zv);
1742 zvol_free(zv);
1743 break;
1744 }
1745 }
1746
1747 mutex_exit(&zvol_state_lock);
1748 }
1749
1750 /*
1751 * Rename minors for specified dataset including children and snapshots.
1752 */
1753 static void
1754 zvol_rename_minors_impl(const char *oldname, const char *newname)
1755 {
1756 zvol_state_t *zv, *zv_next;
1757 int oldnamelen, newnamelen;
1758 char *name;
1759
1760 if (zvol_inhibit_dev)
1761 return;
1762
1763 oldnamelen = strlen(oldname);
1764 newnamelen = strlen(newname);
1765 name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1766
1767 mutex_enter(&zvol_state_lock);
1768
1769 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1770 zv_next = list_next(&zvol_state_list, zv);
1771
1772 /* If in use, leave alone */
1773 if (zv->zv_open_count > 0)
1774 continue;
1775
1776 if (strcmp(zv->zv_name, oldname) == 0) {
1777 zvol_rename_minor(zv, newname);
1778 } else if (strncmp(zv->zv_name, oldname, oldnamelen) == 0 &&
1779 (zv->zv_name[oldnamelen] == '/' ||
1780 zv->zv_name[oldnamelen] == '@')) {
1781 snprintf(name, MAXNAMELEN, "%s%c%s", newname,
1782 zv->zv_name[oldnamelen],
1783 zv->zv_name + oldnamelen + 1);
1784 zvol_rename_minor(zv, name);
1785 }
1786 }
1787
1788 mutex_exit(&zvol_state_lock);
1789
1790 kmem_free(name, MAXNAMELEN);
1791 }
1792
1793 typedef struct zvol_snapdev_cb_arg {
1794 uint64_t snapdev;
1795 } zvol_snapdev_cb_arg_t;
1796
1797 static int
1798 zvol_set_snapdev_cb(const char *dsname, void *param) {
1799 zvol_snapdev_cb_arg_t *arg = param;
1800
1801 if (strchr(dsname, '@') == NULL)
1802 return (0);
1803
1804 switch (arg->snapdev) {
1805 case ZFS_SNAPDEV_VISIBLE:
1806 (void) zvol_create_minor_impl(dsname);
1807 break;
1808 case ZFS_SNAPDEV_HIDDEN:
1809 (void) zvol_remove_minor_impl(dsname);
1810 break;
1811 }
1812
1813 return (0);
1814 }
1815
1816 static void
1817 zvol_set_snapdev_impl(char *name, uint64_t snapdev)
1818 {
1819 zvol_snapdev_cb_arg_t arg = {snapdev};
1820 fstrans_cookie_t cookie = spl_fstrans_mark();
1821 /*
1822 * The zvol_set_snapdev_sync() sets snapdev appropriately
1823 * in the dataset hierarchy. Here, we only scan snapshots.
1824 */
1825 dmu_objset_find(name, zvol_set_snapdev_cb, &arg, DS_FIND_SNAPSHOTS);
1826 spl_fstrans_unmark(cookie);
1827 }
1828
1829 static zvol_task_t *
1830 zvol_task_alloc(zvol_async_op_t op, const char *name1, const char *name2,
1831 uint64_t snapdev)
1832 {
1833 zvol_task_t *task;
1834 char *delim;
1835
1836 /* Never allow tasks on hidden names. */
1837 if (name1[0] == '$')
1838 return (NULL);
1839
1840 task = kmem_zalloc(sizeof (zvol_task_t), KM_SLEEP);
1841 task->op = op;
1842 task->snapdev = snapdev;
1843 delim = strchr(name1, '/');
1844 strlcpy(task->pool, name1, delim ? (delim - name1 + 1) : MAXNAMELEN);
1845
1846 strlcpy(task->name1, name1, MAXNAMELEN);
1847 if (name2 != NULL)
1848 strlcpy(task->name2, name2, MAXNAMELEN);
1849
1850 return (task);
1851 }
1852
1853 static void
1854 zvol_task_free(zvol_task_t *task)
1855 {
1856 kmem_free(task, sizeof (zvol_task_t));
1857 }
1858
1859 /*
1860 * The worker thread function performed asynchronously.
1861 */
1862 static void
1863 zvol_task_cb(void *param)
1864 {
1865 zvol_task_t *task = (zvol_task_t *)param;
1866
1867 switch (task->op) {
1868 case ZVOL_ASYNC_CREATE_MINORS:
1869 (void) zvol_create_minors_impl(task->name1);
1870 break;
1871 case ZVOL_ASYNC_REMOVE_MINORS:
1872 zvol_remove_minors_impl(task->name1);
1873 break;
1874 case ZVOL_ASYNC_RENAME_MINORS:
1875 zvol_rename_minors_impl(task->name1, task->name2);
1876 break;
1877 case ZVOL_ASYNC_SET_SNAPDEV:
1878 zvol_set_snapdev_impl(task->name1, task->snapdev);
1879 break;
1880 default:
1881 VERIFY(0);
1882 break;
1883 }
1884
1885 zvol_task_free(task);
1886 }
1887
1888 typedef struct zvol_set_snapdev_arg {
1889 const char *zsda_name;
1890 uint64_t zsda_value;
1891 zprop_source_t zsda_source;
1892 dmu_tx_t *zsda_tx;
1893 } zvol_set_snapdev_arg_t;
1894
1895 /*
1896 * Sanity check the dataset for safe use by the sync task. No additional
1897 * conditions are imposed.
1898 */
1899 static int
1900 zvol_set_snapdev_check(void *arg, dmu_tx_t *tx)
1901 {
1902 zvol_set_snapdev_arg_t *zsda = arg;
1903 dsl_pool_t *dp = dmu_tx_pool(tx);
1904 dsl_dir_t *dd;
1905 int error;
1906
1907 error = dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL);
1908 if (error != 0)
1909 return (error);
1910
1911 dsl_dir_rele(dd, FTAG);
1912
1913 return (error);
1914 }
1915
1916 static int
1917 zvol_set_snapdev_sync_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
1918 {
1919 zvol_set_snapdev_arg_t *zsda = arg;
1920 char dsname[MAXNAMELEN];
1921 zvol_task_t *task;
1922
1923 dsl_dataset_name(ds, dsname);
1924 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_SNAPDEV),
1925 zsda->zsda_source, sizeof (zsda->zsda_value), 1,
1926 &zsda->zsda_value, zsda->zsda_tx);
1927
1928 task = zvol_task_alloc(ZVOL_ASYNC_SET_SNAPDEV, dsname,
1929 NULL, zsda->zsda_value);
1930 if (task == NULL)
1931 return (0);
1932
1933 (void) taskq_dispatch(dp->dp_spa->spa_zvol_taskq, zvol_task_cb,
1934 task, TQ_SLEEP);
1935 return (0);
1936 }
1937
1938 /*
1939 * Traverse all child snapshot datasets and apply snapdev appropriately.
1940 */
1941 static void
1942 zvol_set_snapdev_sync(void *arg, dmu_tx_t *tx)
1943 {
1944 zvol_set_snapdev_arg_t *zsda = arg;
1945 dsl_pool_t *dp = dmu_tx_pool(tx);
1946 dsl_dir_t *dd;
1947
1948 VERIFY0(dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL));
1949 zsda->zsda_tx = tx;
1950
1951 dmu_objset_find_dp(dp, dd->dd_object, zvol_set_snapdev_sync_cb,
1952 zsda, DS_FIND_CHILDREN);
1953
1954 dsl_dir_rele(dd, FTAG);
1955 }
1956
1957 int
1958 zvol_set_snapdev(const char *ddname, zprop_source_t source, uint64_t snapdev)
1959 {
1960 zvol_set_snapdev_arg_t zsda;
1961
1962 zsda.zsda_name = ddname;
1963 zsda.zsda_source = source;
1964 zsda.zsda_value = snapdev;
1965
1966 return (dsl_sync_task(ddname, zvol_set_snapdev_check,
1967 zvol_set_snapdev_sync, &zsda, 0, ZFS_SPACE_CHECK_NONE));
1968 }
1969
1970 void
1971 zvol_create_minors(spa_t *spa, const char *name, boolean_t async)
1972 {
1973 zvol_task_t *task;
1974 taskqid_t id;
1975
1976 task = zvol_task_alloc(ZVOL_ASYNC_CREATE_MINORS, name, NULL, ~0ULL);
1977 if (task == NULL)
1978 return;
1979
1980 id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP);
1981 if ((async == B_FALSE) && (id != TASKQID_INVALID))
1982 taskq_wait_id(spa->spa_zvol_taskq, id);
1983 }
1984
1985 void
1986 zvol_remove_minors(spa_t *spa, const char *name, boolean_t async)
1987 {
1988 zvol_task_t *task;
1989 taskqid_t id;
1990
1991 task = zvol_task_alloc(ZVOL_ASYNC_REMOVE_MINORS, name, NULL, ~0ULL);
1992 if (task == NULL)
1993 return;
1994
1995 id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP);
1996 if ((async == B_FALSE) && (id != TASKQID_INVALID))
1997 taskq_wait_id(spa->spa_zvol_taskq, id);
1998 }
1999
2000 void
2001 zvol_rename_minors(spa_t *spa, const char *name1, const char *name2,
2002 boolean_t async)
2003 {
2004 zvol_task_t *task;
2005 taskqid_t id;
2006
2007 task = zvol_task_alloc(ZVOL_ASYNC_RENAME_MINORS, name1, name2, ~0ULL);
2008 if (task == NULL)
2009 return;
2010
2011 id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP);
2012 if ((async == B_FALSE) && (id != TASKQID_INVALID))
2013 taskq_wait_id(spa->spa_zvol_taskq, id);
2014 }
2015
2016 int
2017 zvol_init(void)
2018 {
2019 int i, error;
2020
2021 list_create(&zvol_state_list, sizeof (zvol_state_t),
2022 offsetof(zvol_state_t, zv_next));
2023 mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
2024
2025 zvol_htable = kmem_alloc(ZVOL_HT_SIZE * sizeof (struct hlist_head),
2026 KM_SLEEP);
2027 if (!zvol_htable) {
2028 error = ENOMEM;
2029 goto out;
2030 }
2031 for (i = 0; i < ZVOL_HT_SIZE; i++)
2032 INIT_HLIST_HEAD(&zvol_htable[i]);
2033
2034 error = register_blkdev(zvol_major, ZVOL_DRIVER);
2035 if (error) {
2036 printk(KERN_INFO "ZFS: register_blkdev() failed %d\n", error);
2037 goto out_free;
2038 }
2039
2040 blk_register_region(MKDEV(zvol_major, 0), 1UL << MINORBITS,
2041 THIS_MODULE, zvol_probe, NULL, NULL);
2042
2043 return (0);
2044
2045 out_free:
2046 kmem_free(zvol_htable, ZVOL_HT_SIZE * sizeof (struct hlist_head));
2047 out:
2048 mutex_destroy(&zvol_state_lock);
2049 list_destroy(&zvol_state_list);
2050
2051 return (SET_ERROR(error));
2052 }
2053
2054 void
2055 zvol_fini(void)
2056 {
2057 zvol_remove_minors_impl(NULL);
2058
2059 blk_unregister_region(MKDEV(zvol_major, 0), 1UL << MINORBITS);
2060 unregister_blkdev(zvol_major, ZVOL_DRIVER);
2061 kmem_free(zvol_htable, ZVOL_HT_SIZE * sizeof (struct hlist_head));
2062
2063 list_destroy(&zvol_state_list);
2064 mutex_destroy(&zvol_state_lock);
2065 }
2066
2067 module_param(zvol_inhibit_dev, uint, 0644);
2068 MODULE_PARM_DESC(zvol_inhibit_dev, "Do not create zvol device nodes");
2069
2070 module_param(zvol_major, uint, 0444);
2071 MODULE_PARM_DESC(zvol_major, "Major number for zvol device");
2072
2073 module_param(zvol_max_discard_blocks, ulong, 0444);
2074 MODULE_PARM_DESC(zvol_max_discard_blocks, "Max number of blocks to discard");
2075
2076 module_param(zvol_prefetch_bytes, uint, 0644);
2077 MODULE_PARM_DESC(zvol_prefetch_bytes, "Prefetch N bytes at zvol start+end");