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