]> git.proxmox.com Git - mirror_zfs-debian.git/blame - module/zfs/zvol.c
Imported Upstream version 0.6.2
[mirror_zfs-debian.git] / module / zfs / zvol.c
CommitLineData
60101509
BB
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright (C) 2008-2010 Lawrence Livermore National Security, LLC.
23 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
24 * Rewritten for Linux by Brian Behlendorf <behlendorf1@llnl.gov>.
25 * LLNL-CODE-403049.
26 *
27 * ZFS volume emulation driver.
28 *
29 * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes.
30 * Volumes are accessed through the symbolic links named:
31 *
32 * /dev/<pool_name>/<dataset_name>
33 *
34 * Volumes are persistent through reboot and module load. No user command
35 * needs to be run before opening and using a device.
36 */
37
38#include <sys/dmu_traverse.h>
39#include <sys/dsl_dataset.h>
40#include <sys/dsl_prop.h>
41#include <sys/zap.h>
42#include <sys/zil_impl.h>
43#include <sys/zio.h>
44#include <sys/zfs_rlock.h>
45#include <sys/zfs_znode.h>
46#include <sys/zvol.h>
61e90960 47#include <linux/blkdev_compat.h>
60101509 48
74497b7a 49unsigned int zvol_inhibit_dev = 0;
60101509 50unsigned int zvol_major = ZVOL_MAJOR;
dde9380a 51unsigned int zvol_threads = 32;
7c0e5708 52unsigned long zvol_max_discard_blocks = 16384;
60101509
BB
53
54static taskq_t *zvol_taskq;
55static kmutex_t zvol_state_lock;
56static list_t zvol_state_list;
57static char *zvol_tag = "zvol_tag";
58
59/*
60 * The in-core state of each volume.
61 */
62typedef struct zvol_state {
4c0d8e50 63 char zv_name[MAXNAMELEN]; /* name */
60101509
BB
64 uint64_t zv_volsize; /* advertised space */
65 uint64_t zv_volblocksize;/* volume block size */
66 objset_t *zv_objset; /* objset handle */
67 uint32_t zv_flags; /* ZVOL_* flags */
68 uint32_t zv_open_count; /* open counts */
69 uint32_t zv_changed; /* disk changed */
70 zilog_t *zv_zilog; /* ZIL handle */
71 znode_t zv_znode; /* for range locking */
72 dmu_buf_t *zv_dbuf; /* bonus handle */
73 dev_t zv_dev; /* device id */
74 struct gendisk *zv_disk; /* generic disk */
75 struct request_queue *zv_queue; /* request queue */
76 spinlock_t zv_lock; /* request queue lock */
77 list_node_t zv_next; /* next zvol_state_t linkage */
78} zvol_state_t;
79
80#define ZVOL_RDONLY 0x1
81
82/*
83 * Find the next available range of ZVOL_MINORS minor numbers. The
84 * zvol_state_list is kept in ascending minor order so we simply need
85 * to scan the list for the first gap in the sequence. This allows us
86 * to recycle minor number as devices are created and removed.
87 */
88static int
89zvol_find_minor(unsigned *minor)
90{
91 zvol_state_t *zv;
92
93 *minor = 0;
94 ASSERT(MUTEX_HELD(&zvol_state_lock));
95 for (zv = list_head(&zvol_state_list); zv != NULL;
96 zv = list_next(&zvol_state_list, zv), *minor += ZVOL_MINORS) {
97 if (MINOR(zv->zv_dev) != MINOR(*minor))
98 break;
99 }
100
101 /* All minors are in use */
102 if (*minor >= (1 << MINORBITS))
103 return ENXIO;
104
105 return 0;
106}
107
108/*
109 * Find a zvol_state_t given the full major+minor dev_t.
110 */
111static zvol_state_t *
112zvol_find_by_dev(dev_t dev)
113{
114 zvol_state_t *zv;
115
116 ASSERT(MUTEX_HELD(&zvol_state_lock));
117 for (zv = list_head(&zvol_state_list); zv != NULL;
118 zv = list_next(&zvol_state_list, zv)) {
119 if (zv->zv_dev == dev)
120 return zv;
121 }
122
123 return NULL;
124}
125
126/*
127 * Find a zvol_state_t given the name provided at zvol_alloc() time.
128 */
129static zvol_state_t *
130zvol_find_by_name(const char *name)
131{
132 zvol_state_t *zv;
133
134 ASSERT(MUTEX_HELD(&zvol_state_lock));
135 for (zv = list_head(&zvol_state_list); zv != NULL;
136 zv = list_next(&zvol_state_list, zv)) {
4c0d8e50 137 if (!strncmp(zv->zv_name, name, MAXNAMELEN))
60101509
BB
138 return zv;
139 }
140
141 return NULL;
142}
143
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 */
b01615d5
RY
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 */
60101509
BB
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);
a4430fce
GW
944 if (ro || dmu_objset_is_snapshot(os) ||
945 !spa_writeable(dmu_objset_spa(os))) {
babf3f9b
MM
946 set_disk_ro(zv->zv_disk, 1);
947 zv->zv_flags |= ZVOL_RDONLY;
60101509 948 } else {
babf3f9b
MM
949 set_disk_ro(zv->zv_disk, 0);
950 zv->zv_flags &= ~ZVOL_RDONLY;
60101509
BB
951 }
952
babf3f9b
MM
953out_mutex:
954 if (locked)
955 mutex_exit(&spa_namespace_lock);
956
60101509
BB
957 return (-error);
958}
959
960static void
961zvol_last_close(zvol_state_t *zv)
962{
963 zil_close(zv->zv_zilog);
964 zv->zv_zilog = NULL;
04434775 965
60101509
BB
966 dmu_buf_rele(zv->zv_dbuf, zvol_tag);
967 zv->zv_dbuf = NULL;
04434775
MA
968
969 /*
970 * Evict cached data
971 */
972 if (dsl_dataset_is_dirty(dmu_objset_ds(zv->zv_objset)) &&
973 !(zv->zv_flags & ZVOL_RDONLY))
974 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
975 (void) dmu_objset_evict_dbufs(zv->zv_objset);
976
60101509
BB
977 dmu_objset_disown(zv->zv_objset, zvol_tag);
978 zv->zv_objset = NULL;
979}
980
981static int
982zvol_open(struct block_device *bdev, fmode_t flag)
983{
984 zvol_state_t *zv = bdev->bd_disk->private_data;
985 int error = 0, drop_mutex = 0;
986
987 /*
988 * If the caller is already holding the mutex do not take it
989 * again, this will happen as part of zvol_create_minor().
990 * Once add_disk() is called the device is live and the kernel
991 * will attempt to open it to read the partition information.
992 */
993 if (!mutex_owned(&zvol_state_lock)) {
994 mutex_enter(&zvol_state_lock);
995 drop_mutex = 1;
996 }
997
998 ASSERT3P(zv, !=, NULL);
999
1000 if (zv->zv_open_count == 0) {
1001 error = zvol_first_open(zv);
1002 if (error)
1003 goto out_mutex;
1004 }
1005
1006 if ((flag & FMODE_WRITE) &&
1007 (get_disk_ro(zv->zv_disk) || (zv->zv_flags & ZVOL_RDONLY))) {
1008 error = -EROFS;
1009 goto out_open_count;
1010 }
1011
1012 zv->zv_open_count++;
1013
1014out_open_count:
1015 if (zv->zv_open_count == 0)
1016 zvol_last_close(zv);
1017
1018out_mutex:
1019 if (drop_mutex)
1020 mutex_exit(&zvol_state_lock);
1021
1022 check_disk_change(bdev);
1023
1024 return (error);
1025}
1026
c06d4368
AX
1027#ifdef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
1028static void
1029#else
60101509 1030static int
c06d4368 1031#endif
60101509
BB
1032zvol_release(struct gendisk *disk, fmode_t mode)
1033{
1034 zvol_state_t *zv = disk->private_data;
1035 int drop_mutex = 0;
1036
1037 if (!mutex_owned(&zvol_state_lock)) {
1038 mutex_enter(&zvol_state_lock);
1039 drop_mutex = 1;
1040 }
1041
1042 ASSERT3P(zv, !=, NULL);
1043 ASSERT3U(zv->zv_open_count, >, 0);
1044 zv->zv_open_count--;
1045 if (zv->zv_open_count == 0)
1046 zvol_last_close(zv);
1047
1048 if (drop_mutex)
1049 mutex_exit(&zvol_state_lock);
1050
c06d4368 1051#ifndef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
60101509 1052 return (0);
c06d4368 1053#endif
60101509
BB
1054}
1055
1056static int
1057zvol_ioctl(struct block_device *bdev, fmode_t mode,
1058 unsigned int cmd, unsigned long arg)
1059{
1060 zvol_state_t *zv = bdev->bd_disk->private_data;
1061 int error = 0;
1062
1063 if (zv == NULL)
1064 return (-ENXIO);
1065
1066 switch (cmd) {
1067 case BLKFLSBUF:
1068 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1069 break;
4c0d8e50
FN
1070 case BLKZNAME:
1071 error = copy_to_user((void *)arg, zv->zv_name, MAXNAMELEN);
1072 break;
60101509
BB
1073
1074 default:
1075 error = -ENOTTY;
1076 break;
1077
1078 }
1079
1080 return (error);
1081}
1082
1083#ifdef CONFIG_COMPAT
1084static int
1085zvol_compat_ioctl(struct block_device *bdev, fmode_t mode,
1086 unsigned cmd, unsigned long arg)
1087{
1088 return zvol_ioctl(bdev, mode, cmd, arg);
1089}
1090#else
1091#define zvol_compat_ioctl NULL
1092#endif
1093
1094static int zvol_media_changed(struct gendisk *disk)
1095{
1096 zvol_state_t *zv = disk->private_data;
1097
1098 return zv->zv_changed;
1099}
1100
1101static int zvol_revalidate_disk(struct gendisk *disk)
1102{
1103 zvol_state_t *zv = disk->private_data;
1104
1105 zv->zv_changed = 0;
1106 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1107
1108 return 0;
1109}
1110
1111/*
1112 * Provide a simple virtual geometry for legacy compatibility. For devices
1113 * smaller than 1 MiB a small head and sector count is used to allow very
1114 * tiny devices. For devices over 1 Mib a standard head and sector count
1115 * is used to keep the cylinders count reasonable.
1116 */
1117static int
1118zvol_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1119{
1120 zvol_state_t *zv = bdev->bd_disk->private_data;
1121 sector_t sectors = get_capacity(zv->zv_disk);
1122
1123 if (sectors > 2048) {
1124 geo->heads = 16;
1125 geo->sectors = 63;
1126 } else {
1127 geo->heads = 2;
1128 geo->sectors = 4;
1129 }
1130
1131 geo->start = 0;
1132 geo->cylinders = sectors / (geo->heads * geo->sectors);
1133
1134 return 0;
1135}
1136
1137static struct kobject *
1138zvol_probe(dev_t dev, int *part, void *arg)
1139{
1140 zvol_state_t *zv;
1141 struct kobject *kobj;
1142
1143 mutex_enter(&zvol_state_lock);
1144 zv = zvol_find_by_dev(dev);
23a61ccc 1145 kobj = zv ? get_disk(zv->zv_disk) : NULL;
60101509
BB
1146 mutex_exit(&zvol_state_lock);
1147
1148 return kobj;
1149}
1150
1151#ifdef HAVE_BDEV_BLOCK_DEVICE_OPERATIONS
1152static struct block_device_operations zvol_ops = {
1153 .open = zvol_open,
1154 .release = zvol_release,
1155 .ioctl = zvol_ioctl,
1156 .compat_ioctl = zvol_compat_ioctl,
1157 .media_changed = zvol_media_changed,
1158 .revalidate_disk = zvol_revalidate_disk,
1159 .getgeo = zvol_getgeo,
1160 .owner = THIS_MODULE,
1161};
1162
1163#else /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1164
1165static int
1166zvol_open_by_inode(struct inode *inode, struct file *file)
1167{
1168 return zvol_open(inode->i_bdev, file->f_mode);
1169}
1170
1171static int
1172zvol_release_by_inode(struct inode *inode, struct file *file)
1173{
1174 return zvol_release(inode->i_bdev->bd_disk, file->f_mode);
1175}
1176
1177static int
1178zvol_ioctl_by_inode(struct inode *inode, struct file *file,
1179 unsigned int cmd, unsigned long arg)
1180{
b1c58213
NB
1181 if (file == NULL || inode == NULL)
1182 return -EINVAL;
60101509
BB
1183 return zvol_ioctl(inode->i_bdev, file->f_mode, cmd, arg);
1184}
1185
1186# ifdef CONFIG_COMPAT
1187static long
1188zvol_compat_ioctl_by_inode(struct file *file,
1189 unsigned int cmd, unsigned long arg)
1190{
b1c58213
NB
1191 if (file == NULL)
1192 return -EINVAL;
60101509
BB
1193 return zvol_compat_ioctl(file->f_dentry->d_inode->i_bdev,
1194 file->f_mode, cmd, arg);
1195}
1196# else
1197# define zvol_compat_ioctl_by_inode NULL
1198# endif
1199
1200static struct block_device_operations zvol_ops = {
1201 .open = zvol_open_by_inode,
1202 .release = zvol_release_by_inode,
1203 .ioctl = zvol_ioctl_by_inode,
1204 .compat_ioctl = zvol_compat_ioctl_by_inode,
1205 .media_changed = zvol_media_changed,
1206 .revalidate_disk = zvol_revalidate_disk,
1207 .getgeo = zvol_getgeo,
1208 .owner = THIS_MODULE,
1209};
1210#endif /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1211
1212/*
1213 * Allocate memory for a new zvol_state_t and setup the required
1214 * request queue and generic disk structures for the block device.
1215 */
1216static zvol_state_t *
1217zvol_alloc(dev_t dev, const char *name)
1218{
1219 zvol_state_t *zv;
7bd04f2d 1220 int error = 0;
60101509
BB
1221
1222 zv = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
c06d4368
AX
1223
1224 spin_lock_init(&zv->zv_lock);
1225 list_link_init(&zv->zv_next);
60101509
BB
1226
1227 zv->zv_queue = blk_init_queue(zvol_request, &zv->zv_lock);
1228 if (zv->zv_queue == NULL)
1229 goto out_kmem;
1230
7bd04f2d
BB
1231#ifdef HAVE_ELEVATOR_CHANGE
1232 error = elevator_change(zv->zv_queue, "noop");
1233#endif /* HAVE_ELEVATOR_CHANGE */
1234 if (error) {
1235 printk("ZFS: Unable to set \"%s\" scheduler for zvol %s: %d\n",
1236 "noop", name, error);
1237 goto out_queue;
1238 }
1239
b18019d2
ED
1240#ifdef HAVE_BLK_QUEUE_FLUSH
1241 blk_queue_flush(zv->zv_queue, VDEV_REQ_FLUSH | VDEV_REQ_FUA);
1242#else
1243 blk_queue_ordered(zv->zv_queue, QUEUE_ORDERED_DRAIN, NULL);
1244#endif /* HAVE_BLK_QUEUE_FLUSH */
1245
60101509
BB
1246 zv->zv_disk = alloc_disk(ZVOL_MINORS);
1247 if (zv->zv_disk == NULL)
1248 goto out_queue;
1249
1250 zv->zv_queue->queuedata = zv;
1251 zv->zv_dev = dev;
1252 zv->zv_open_count = 0;
4c0d8e50 1253 strlcpy(zv->zv_name, name, MAXNAMELEN);
60101509
BB
1254
1255 mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
1256 avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
1257 sizeof (rl_t), offsetof(rl_t, r_node));
3c4988c8
BB
1258 zv->zv_znode.z_is_zvol = TRUE;
1259
60101509
BB
1260 zv->zv_disk->major = zvol_major;
1261 zv->zv_disk->first_minor = (dev & MINORMASK);
1262 zv->zv_disk->fops = &zvol_ops;
1263 zv->zv_disk->private_data = zv;
1264 zv->zv_disk->queue = zv->zv_queue;
4c0d8e50
FN
1265 snprintf(zv->zv_disk->disk_name, DISK_NAME_LEN, "%s%d",
1266 ZVOL_DEV_NAME, (dev & MINORMASK));
60101509
BB
1267
1268 return zv;
1269
1270out_queue:
1271 blk_cleanup_queue(zv->zv_queue);
1272out_kmem:
1273 kmem_free(zv, sizeof (zvol_state_t));
c06d4368 1274
60101509
BB
1275 return NULL;
1276}
1277
1278/*
1279 * Cleanup then free a zvol_state_t which was created by zvol_alloc().
1280 */
1281static void
1282zvol_free(zvol_state_t *zv)
1283{
1284 avl_destroy(&zv->zv_znode.z_range_avl);
1285 mutex_destroy(&zv->zv_znode.z_range_lock);
1286
1287 del_gendisk(zv->zv_disk);
1288 blk_cleanup_queue(zv->zv_queue);
1289 put_disk(zv->zv_disk);
1290
1291 kmem_free(zv, sizeof (zvol_state_t));
1292}
1293
1294static int
0b4d1b58
ED
1295__zvol_snapdev_hidden(const char *name)
1296{
1297 uint64_t snapdev;
1298 char *parent;
1299 char *atp;
1300 int error = 0;
1301
1302 parent = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1303 (void) strlcpy(parent, name, MAXPATHLEN);
1304
1305 if ((atp = strrchr(parent, '@')) != NULL) {
1306 *atp = '\0';
1307 error = dsl_prop_get_integer(parent, "snapdev", &snapdev, NULL);
1308 if ((error == 0) && (snapdev == ZFS_SNAPDEV_HIDDEN))
1309 error = ENODEV;
1310 }
1311 kmem_free(parent, MAXPATHLEN);
1312 return (error);
1313}
1314
1315static int
1316__zvol_create_minor(const char *name, boolean_t ignore_snapdev)
60101509
BB
1317{
1318 zvol_state_t *zv;
1319 objset_t *os;
1320 dmu_object_info_t *doi;
1321 uint64_t volsize;
1322 unsigned minor = 0;
1323 int error = 0;
1324
1325 ASSERT(MUTEX_HELD(&zvol_state_lock));
1326
1327 zv = zvol_find_by_name(name);
1328 if (zv) {
1329 error = EEXIST;
1330 goto out;
1331 }
1332
0b4d1b58
ED
1333 if (ignore_snapdev == B_FALSE) {
1334 error = __zvol_snapdev_hidden(name);
1335 if (error)
1336 goto out;
1337 }
1338
60101509
BB
1339 doi = kmem_alloc(sizeof(dmu_object_info_t), KM_SLEEP);
1340
1341 error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os);
1342 if (error)
1343 goto out_doi;
1344
1345 error = dmu_object_info(os, ZVOL_OBJ, doi);
1346 if (error)
1347 goto out_dmu_objset_disown;
1348
1349 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
1350 if (error)
1351 goto out_dmu_objset_disown;
1352
1353 error = zvol_find_minor(&minor);
1354 if (error)
1355 goto out_dmu_objset_disown;
1356
1357 zv = zvol_alloc(MKDEV(zvol_major, minor), name);
1358 if (zv == NULL) {
1359 error = EAGAIN;
1360 goto out_dmu_objset_disown;
1361 }
1362
1363 if (dmu_objset_is_snapshot(os))
1364 zv->zv_flags |= ZVOL_RDONLY;
1365
1366 zv->zv_volblocksize = doi->doi_data_block_size;
1367 zv->zv_volsize = volsize;
1368 zv->zv_objset = os;
1369
1370 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1371
34037afe
ED
1372 blk_queue_max_hw_sectors(zv->zv_queue, UINT_MAX);
1373 blk_queue_max_segments(zv->zv_queue, UINT16_MAX);
1374 blk_queue_max_segment_size(zv->zv_queue, UINT_MAX);
1375 blk_queue_physical_block_size(zv->zv_queue, zv->zv_volblocksize);
1376 blk_queue_io_opt(zv->zv_queue, zv->zv_volblocksize);
30930fba 1377#ifdef HAVE_BLK_QUEUE_DISCARD
7c0e5708
ED
1378 blk_queue_max_discard_sectors(zv->zv_queue,
1379 (zvol_max_discard_blocks * zv->zv_volblocksize) >> 9);
ee5fd0bb 1380 blk_queue_discard_granularity(zv->zv_queue, zv->zv_volblocksize);
30930fba
ED
1381 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, zv->zv_queue);
1382#endif
34037afe
ED
1383#ifdef HAVE_BLK_QUEUE_NONROT
1384 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zv->zv_queue);
1385#endif
1386
a4430fce
GW
1387 if (spa_writeable(dmu_objset_spa(os))) {
1388 if (zil_replay_disable)
1389 zil_destroy(dmu_objset_zil(os), B_FALSE);
1390 else
1391 zil_replay(os, zv, zvol_replay_vector);
1392 }
60101509 1393
f74a147c 1394 zv->zv_objset = NULL;
60101509
BB
1395out_dmu_objset_disown:
1396 dmu_objset_disown(os, zvol_tag);
60101509
BB
1397out_doi:
1398 kmem_free(doi, sizeof(dmu_object_info_t));
1399out:
1400
1401 if (error == 0) {
1402 zvol_insert(zv);
1403 add_disk(zv->zv_disk);
1404 }
1405
1406 return (error);
1407}
1408
1409/*
1410 * Create a block device minor node and setup the linkage between it
1411 * and the specified volume. Once this function returns the block
1412 * device is live and ready for use.
1413 */
1414int
1415zvol_create_minor(const char *name)
1416{
1417 int error;
1418
1419 mutex_enter(&zvol_state_lock);
0b4d1b58 1420 error = __zvol_create_minor(name, B_FALSE);
60101509
BB
1421 mutex_exit(&zvol_state_lock);
1422
1423 return (error);
1424}
1425
1426static int
1427__zvol_remove_minor(const char *name)
1428{
1429 zvol_state_t *zv;
1430
1431 ASSERT(MUTEX_HELD(&zvol_state_lock));
1432
1433 zv = zvol_find_by_name(name);
1434 if (zv == NULL)
1435 return (ENXIO);
1436
1437 if (zv->zv_open_count > 0)
1438 return (EBUSY);
1439
1440 zvol_remove(zv);
1441 zvol_free(zv);
1442
1443 return (0);
1444}
1445
1446/*
1447 * Remove a block device minor node for the specified volume.
1448 */
1449int
1450zvol_remove_minor(const char *name)
1451{
1452 int error;
1453
1454 mutex_enter(&zvol_state_lock);
1455 error = __zvol_remove_minor(name);
1456 mutex_exit(&zvol_state_lock);
1457
1458 return (error);
1459}
1460
1461static int
1462zvol_create_minors_cb(spa_t *spa, uint64_t dsobj,
1463 const char *dsname, void *arg)
1464{
1465 if (strchr(dsname, '/') == NULL)
1466 return 0;
1467
0b4d1b58 1468 (void) __zvol_create_minor(dsname, B_FALSE);
d5674448 1469 return (0);
60101509
BB
1470}
1471
1472/*
1473 * Create minors for specified pool, if pool is NULL create minors
1474 * for all available pools.
1475 */
1476int
1477zvol_create_minors(const char *pool)
1478{
1479 spa_t *spa = NULL;
1480 int error = 0;
1481
74497b7a
DH
1482 if (zvol_inhibit_dev)
1483 return (0);
1484
60101509
BB
1485 mutex_enter(&zvol_state_lock);
1486 if (pool) {
1487 error = dmu_objset_find_spa(NULL, pool, zvol_create_minors_cb,
1488 NULL, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1489 } else {
1490 mutex_enter(&spa_namespace_lock);
1491 while ((spa = spa_next(spa)) != NULL) {
1492 error = dmu_objset_find_spa(NULL,
1493 spa_name(spa), zvol_create_minors_cb, NULL,
1494 DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS);
1495 if (error)
1496 break;
1497 }
1498 mutex_exit(&spa_namespace_lock);
1499 }
1500 mutex_exit(&zvol_state_lock);
1501
1502 return error;
1503}
1504
1505/*
1506 * Remove minors for specified pool, if pool is NULL remove all minors.
1507 */
1508void
1509zvol_remove_minors(const char *pool)
1510{
1511 zvol_state_t *zv, *zv_next;
1512 char *str;
1513
74497b7a
DH
1514 if (zvol_inhibit_dev)
1515 return;
1516
4c0d8e50 1517 str = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
60101509
BB
1518 if (pool) {
1519 (void) strncpy(str, pool, strlen(pool));
1520 (void) strcat(str, "/");
1521 }
1522
1523 mutex_enter(&zvol_state_lock);
1524 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1525 zv_next = list_next(&zvol_state_list, zv);
1526
1527 if (pool == NULL || !strncmp(str, zv->zv_name, strlen(str))) {
1528 zvol_remove(zv);
1529 zvol_free(zv);
1530 }
1531 }
1532 mutex_exit(&zvol_state_lock);
4c0d8e50 1533 kmem_free(str, MAXNAMELEN);
60101509
BB
1534}
1535
0b4d1b58
ED
1536static int
1537snapdev_snapshot_changed_cb(const char *dsname, void *arg) {
1538 uint64_t snapdev = *(uint64_t *) arg;
1539
1540 if (strchr(dsname, '@') == NULL)
1541 return 0;
1542
1543 switch (snapdev) {
1544 case ZFS_SNAPDEV_VISIBLE:
1545 mutex_enter(&zvol_state_lock);
1546 (void) __zvol_create_minor(dsname, B_TRUE);
1547 mutex_exit(&zvol_state_lock);
1548 break;
1549 case ZFS_SNAPDEV_HIDDEN:
1550 (void) zvol_remove_minor(dsname);
1551 break;
1552 }
1553 return 0;
1554}
1555
1556int
1557zvol_set_snapdev(const char *dsname, uint64_t snapdev) {
1558 (void) dmu_objset_find((char *) dsname, snapdev_snapshot_changed_cb,
1559 &snapdev, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
1560 /* caller should continue to modify snapdev property */
1561 return (-1);
1562}
1563
1564
60101509
BB
1565int
1566zvol_init(void)
1567{
1568 int error;
1569
c06d4368
AX
1570 list_create(&zvol_state_list, sizeof (zvol_state_t),
1571 offsetof(zvol_state_t, zv_next));
1572 mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
1573
60101509 1574 zvol_taskq = taskq_create(ZVOL_DRIVER, zvol_threads, maxclsyspri,
71011408 1575 zvol_threads, INT_MAX, TASKQ_PREPOPULATE);
60101509
BB
1576 if (zvol_taskq == NULL) {
1577 printk(KERN_INFO "ZFS: taskq_create() failed\n");
c06d4368
AX
1578 error = -ENOMEM;
1579 goto out1;
60101509
BB
1580 }
1581
1582 error = register_blkdev(zvol_major, ZVOL_DRIVER);
1583 if (error) {
1584 printk(KERN_INFO "ZFS: register_blkdev() failed %d\n", error);
c06d4368 1585 goto out2;
60101509
BB
1586 }
1587
1588 blk_register_region(MKDEV(zvol_major, 0), 1UL << MINORBITS,
1589 THIS_MODULE, zvol_probe, NULL, NULL);
1590
c06d4368 1591 return (0);
60101509 1592
c06d4368
AX
1593out2:
1594 taskq_destroy(zvol_taskq);
1595out1:
1596 mutex_destroy(&zvol_state_lock);
1597 list_destroy(&zvol_state_list);
60101509 1598
c06d4368 1599 return (error);
60101509
BB
1600}
1601
1602void
1603zvol_fini(void)
1604{
1605 zvol_remove_minors(NULL);
1606 blk_unregister_region(MKDEV(zvol_major, 0), 1UL << MINORBITS);
1607 unregister_blkdev(zvol_major, ZVOL_DRIVER);
1608 taskq_destroy(zvol_taskq);
1609 mutex_destroy(&zvol_state_lock);
1610 list_destroy(&zvol_state_list);
1611}
1612
74497b7a
DH
1613module_param(zvol_inhibit_dev, uint, 0644);
1614MODULE_PARM_DESC(zvol_inhibit_dev, "Do not create zvol device nodes");
1615
30a9524e 1616module_param(zvol_major, uint, 0444);
60101509
BB
1617MODULE_PARM_DESC(zvol_major, "Major number for zvol device");
1618
30a9524e 1619module_param(zvol_threads, uint, 0444);
60101509 1620MODULE_PARM_DESC(zvol_threads, "Number of threads for zvol device");
7c0e5708
ED
1621
1622module_param(zvol_max_discard_blocks, ulong, 0444);
1623MODULE_PARM_DESC(zvol_max_discard_blocks, "Max number of blocks to discard at once");