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