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