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