]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zvol.c
Enable Linux read-ahead for a single page on ZVOLs
[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 = 1;
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;
1154
1155 /* lie and say we're read-only */
1156 error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, 1, zv, &os);
1157 if (error)
1158 return (SET_ERROR(-error));
1159
1160 zv->zv_objset = os;
1161
1162 error = zvol_setup_zv(zv);
1163
1164 if (error) {
1165 dmu_objset_disown(os, zv);
1166 zv->zv_objset = NULL;
1167 }
1168
1169 return (SET_ERROR(-error));
1170 }
1171
1172 static void
1173 zvol_last_close(zvol_state_t *zv)
1174 {
1175 zvol_shutdown_zv(zv);
1176
1177 dmu_objset_disown(zv->zv_objset, zv);
1178 zv->zv_objset = NULL;
1179 }
1180
1181 static int
1182 zvol_open(struct block_device *bdev, fmode_t flag)
1183 {
1184 zvol_state_t *zv;
1185 int error = 0, drop_mutex = 0, drop_suspend = 0;
1186
1187 /*
1188 * If the caller is already holding the mutex do not take it
1189 * again, this will happen as part of zvol_create_minor_impl().
1190 * Once add_disk() is called the device is live and the kernel
1191 * will attempt to open it to read the partition information.
1192 */
1193 if (!mutex_owned(&zvol_state_lock)) {
1194 mutex_enter(&zvol_state_lock);
1195 drop_mutex = 1;
1196 }
1197
1198 /*
1199 * Obtain a copy of private_data under the lock to make sure
1200 * that either the result of zvol_free() setting
1201 * bdev->bd_disk->private_data to NULL is observed, or zvol_free()
1202 * is not called on this zv because of the positive zv_open_count.
1203 */
1204 zv = bdev->bd_disk->private_data;
1205 if (zv == NULL) {
1206 error = -ENXIO;
1207 goto out_mutex;
1208 }
1209
1210 if (zv->zv_open_count == 0) {
1211 /* make sure zvol is not suspended when first open */
1212 rw_enter(&zv->zv_suspend_lock, RW_READER);
1213 drop_suspend = 1;
1214
1215 error = zvol_first_open(zv);
1216 if (error)
1217 goto out_mutex;
1218 }
1219
1220 if ((flag & FMODE_WRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
1221 error = -EROFS;
1222 goto out_open_count;
1223 }
1224
1225 zv->zv_open_count++;
1226
1227 check_disk_change(bdev);
1228
1229 out_open_count:
1230 if (zv->zv_open_count == 0)
1231 zvol_last_close(zv);
1232 out_mutex:
1233 if (drop_suspend)
1234 rw_exit(&zv->zv_suspend_lock);
1235 if (drop_mutex)
1236 mutex_exit(&zvol_state_lock);
1237
1238 return (SET_ERROR(error));
1239 }
1240
1241 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
1242 static void
1243 #else
1244 static int
1245 #endif
1246 zvol_release(struct gendisk *disk, fmode_t mode)
1247 {
1248 zvol_state_t *zv = disk->private_data;
1249 int drop_mutex = 0;
1250
1251 ASSERT(zv && zv->zv_open_count > 0);
1252
1253 if (!mutex_owned(&zvol_state_lock)) {
1254 mutex_enter(&zvol_state_lock);
1255 drop_mutex = 1;
1256 }
1257
1258 /* make sure zvol is not suspended when last close */
1259 if (zv->zv_open_count == 1)
1260 rw_enter(&zv->zv_suspend_lock, RW_READER);
1261
1262 zv->zv_open_count--;
1263 if (zv->zv_open_count == 0) {
1264 zvol_last_close(zv);
1265 rw_exit(&zv->zv_suspend_lock);
1266 }
1267
1268 if (drop_mutex)
1269 mutex_exit(&zvol_state_lock);
1270
1271 #ifndef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID
1272 return (0);
1273 #endif
1274 }
1275
1276 static int
1277 zvol_ioctl(struct block_device *bdev, fmode_t mode,
1278 unsigned int cmd, unsigned long arg)
1279 {
1280 zvol_state_t *zv = bdev->bd_disk->private_data;
1281 int error = 0;
1282
1283 ASSERT(zv && zv->zv_open_count > 0);
1284
1285 switch (cmd) {
1286 case BLKFLSBUF:
1287 fsync_bdev(bdev);
1288 invalidate_bdev(bdev);
1289 rw_enter(&zv->zv_suspend_lock, RW_READER);
1290
1291 if (dsl_dataset_is_dirty(dmu_objset_ds(zv->zv_objset)) &&
1292 !(zv->zv_flags & ZVOL_RDONLY))
1293 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
1294
1295 rw_exit(&zv->zv_suspend_lock);
1296 break;
1297
1298 case BLKZNAME:
1299 mutex_enter(&zvol_state_lock);
1300 error = copy_to_user((void *)arg, zv->zv_name, MAXNAMELEN);
1301 mutex_exit(&zvol_state_lock);
1302 break;
1303
1304 default:
1305 error = -ENOTTY;
1306 break;
1307 }
1308
1309 return (SET_ERROR(error));
1310 }
1311
1312 #ifdef CONFIG_COMPAT
1313 static int
1314 zvol_compat_ioctl(struct block_device *bdev, fmode_t mode,
1315 unsigned cmd, unsigned long arg)
1316 {
1317 return (zvol_ioctl(bdev, mode, cmd, arg));
1318 }
1319 #else
1320 #define zvol_compat_ioctl NULL
1321 #endif
1322
1323 static int zvol_media_changed(struct gendisk *disk)
1324 {
1325 zvol_state_t *zv = disk->private_data;
1326
1327 ASSERT(zv && zv->zv_open_count > 0);
1328
1329 return (zv->zv_changed);
1330 }
1331
1332 static int zvol_revalidate_disk(struct gendisk *disk)
1333 {
1334 zvol_state_t *zv = disk->private_data;
1335
1336 ASSERT(zv && zv->zv_open_count > 0);
1337
1338 zv->zv_changed = 0;
1339 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1340
1341 return (0);
1342 }
1343
1344 /*
1345 * Provide a simple virtual geometry for legacy compatibility. For devices
1346 * smaller than 1 MiB a small head and sector count is used to allow very
1347 * tiny devices. For devices over 1 Mib a standard head and sector count
1348 * is used to keep the cylinders count reasonable.
1349 */
1350 static int
1351 zvol_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1352 {
1353 zvol_state_t *zv = bdev->bd_disk->private_data;
1354 sector_t sectors;
1355
1356 ASSERT(zv && zv->zv_open_count > 0);
1357
1358 sectors = get_capacity(zv->zv_disk);
1359
1360 if (sectors > 2048) {
1361 geo->heads = 16;
1362 geo->sectors = 63;
1363 } else {
1364 geo->heads = 2;
1365 geo->sectors = 4;
1366 }
1367
1368 geo->start = 0;
1369 geo->cylinders = sectors / (geo->heads * geo->sectors);
1370
1371 return (0);
1372 }
1373
1374 static struct kobject *
1375 zvol_probe(dev_t dev, int *part, void *arg)
1376 {
1377 zvol_state_t *zv;
1378 struct kobject *kobj;
1379
1380 mutex_enter(&zvol_state_lock);
1381 zv = zvol_find_by_dev(dev);
1382 kobj = zv ? get_disk(zv->zv_disk) : NULL;
1383 mutex_exit(&zvol_state_lock);
1384
1385 return (kobj);
1386 }
1387
1388 #ifdef HAVE_BDEV_BLOCK_DEVICE_OPERATIONS
1389 static struct block_device_operations zvol_ops = {
1390 .open = zvol_open,
1391 .release = zvol_release,
1392 .ioctl = zvol_ioctl,
1393 .compat_ioctl = zvol_compat_ioctl,
1394 .media_changed = zvol_media_changed,
1395 .revalidate_disk = zvol_revalidate_disk,
1396 .getgeo = zvol_getgeo,
1397 .owner = THIS_MODULE,
1398 };
1399
1400 #else /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1401
1402 static int
1403 zvol_open_by_inode(struct inode *inode, struct file *file)
1404 {
1405 return (zvol_open(inode->i_bdev, file->f_mode));
1406 }
1407
1408 static int
1409 zvol_release_by_inode(struct inode *inode, struct file *file)
1410 {
1411 return (zvol_release(inode->i_bdev->bd_disk, file->f_mode));
1412 }
1413
1414 static int
1415 zvol_ioctl_by_inode(struct inode *inode, struct file *file,
1416 unsigned int cmd, unsigned long arg)
1417 {
1418 if (file == NULL || inode == NULL)
1419 return (SET_ERROR(-EINVAL));
1420
1421 return (zvol_ioctl(inode->i_bdev, file->f_mode, cmd, arg));
1422 }
1423
1424 #ifdef CONFIG_COMPAT
1425 static long
1426 zvol_compat_ioctl_by_inode(struct file *file,
1427 unsigned int cmd, unsigned long arg)
1428 {
1429 if (file == NULL)
1430 return (SET_ERROR(-EINVAL));
1431
1432 return (zvol_compat_ioctl(file->f_dentry->d_inode->i_bdev,
1433 file->f_mode, cmd, arg));
1434 }
1435 #else
1436 #define zvol_compat_ioctl_by_inode NULL
1437 #endif
1438
1439 static struct block_device_operations zvol_ops = {
1440 .open = zvol_open_by_inode,
1441 .release = zvol_release_by_inode,
1442 .ioctl = zvol_ioctl_by_inode,
1443 .compat_ioctl = zvol_compat_ioctl_by_inode,
1444 .media_changed = zvol_media_changed,
1445 .revalidate_disk = zvol_revalidate_disk,
1446 .getgeo = zvol_getgeo,
1447 .owner = THIS_MODULE,
1448 };
1449 #endif /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */
1450
1451 /*
1452 * Allocate memory for a new zvol_state_t and setup the required
1453 * request queue and generic disk structures for the block device.
1454 */
1455 static zvol_state_t *
1456 zvol_alloc(dev_t dev, const char *name)
1457 {
1458 zvol_state_t *zv;
1459
1460 zv = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
1461
1462 list_link_init(&zv->zv_next);
1463
1464 zv->zv_queue = blk_alloc_queue(GFP_ATOMIC);
1465 if (zv->zv_queue == NULL)
1466 goto out_kmem;
1467
1468 blk_queue_make_request(zv->zv_queue, zvol_request);
1469 blk_queue_set_write_cache(zv->zv_queue, B_TRUE, B_TRUE);
1470
1471 /* Limit read-ahead to a single page to prevent over-prefetching. */
1472 blk_queue_set_read_ahead(zv->zv_queue, 1);
1473
1474 /* Disable write merging in favor of the ZIO pipeline. */
1475 queue_flag_set(QUEUE_FLAG_NOMERGES, zv->zv_queue);
1476
1477 zv->zv_disk = alloc_disk(ZVOL_MINORS);
1478 if (zv->zv_disk == NULL)
1479 goto out_queue;
1480
1481 zv->zv_queue->queuedata = zv;
1482 zv->zv_dev = dev;
1483 zv->zv_open_count = 0;
1484 strlcpy(zv->zv_name, name, MAXNAMELEN);
1485
1486 zfs_rlock_init(&zv->zv_range_lock);
1487 rw_init(&zv->zv_suspend_lock, NULL, RW_DEFAULT, NULL);
1488
1489 zv->zv_disk->major = zvol_major;
1490 zv->zv_disk->first_minor = (dev & MINORMASK);
1491 zv->zv_disk->fops = &zvol_ops;
1492 zv->zv_disk->private_data = zv;
1493 zv->zv_disk->queue = zv->zv_queue;
1494 snprintf(zv->zv_disk->disk_name, DISK_NAME_LEN, "%s%d",
1495 ZVOL_DEV_NAME, (dev & MINORMASK));
1496
1497 return (zv);
1498
1499 out_queue:
1500 blk_cleanup_queue(zv->zv_queue);
1501 out_kmem:
1502 kmem_free(zv, sizeof (zvol_state_t));
1503
1504 return (NULL);
1505 }
1506
1507 /*
1508 * Used for taskq, if used out side zvol_state_lock, you need to clear
1509 * zv_disk->private_data inside lock first.
1510 */
1511 static void
1512 zvol_free_impl(void *arg)
1513 {
1514 zvol_state_t *zv = arg;
1515 ASSERT(zv->zv_open_count == 0);
1516
1517 rw_destroy(&zv->zv_suspend_lock);
1518 zfs_rlock_destroy(&zv->zv_range_lock);
1519
1520 zv->zv_disk->private_data = NULL;
1521
1522 del_gendisk(zv->zv_disk);
1523 blk_cleanup_queue(zv->zv_queue);
1524 put_disk(zv->zv_disk);
1525
1526 ida_simple_remove(&zvol_ida, MINOR(zv->zv_dev) >> ZVOL_MINOR_BITS);
1527 kmem_free(zv, sizeof (zvol_state_t));
1528 }
1529
1530 /*
1531 * Cleanup then free a zvol_state_t which was created by zvol_alloc().
1532 */
1533 static void
1534 zvol_free(zvol_state_t *zv)
1535 {
1536 ASSERT(MUTEX_HELD(&zvol_state_lock));
1537 zvol_free_impl(zv);
1538 }
1539
1540 /*
1541 * Create a block device minor node and setup the linkage between it
1542 * and the specified volume. Once this function returns the block
1543 * device is live and ready for use.
1544 */
1545 static int
1546 zvol_create_minor_impl(const char *name)
1547 {
1548 zvol_state_t *zv;
1549 objset_t *os;
1550 dmu_object_info_t *doi;
1551 uint64_t volsize;
1552 uint64_t len;
1553 unsigned minor = 0;
1554 int error = 0;
1555 int idx;
1556 uint64_t hash = zvol_name_hash(name);
1557
1558 idx = ida_simple_get(&zvol_ida, 0, 0, kmem_flags_convert(KM_SLEEP));
1559 if (idx < 0)
1560 return (SET_ERROR(-idx));
1561 minor = idx << ZVOL_MINOR_BITS;
1562
1563 mutex_enter(&zvol_state_lock);
1564
1565 zv = zvol_find_by_name_hash(name, hash);
1566 if (zv) {
1567 error = SET_ERROR(EEXIST);
1568 goto out;
1569 }
1570
1571 doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP);
1572
1573 error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, FTAG, &os);
1574 if (error)
1575 goto out_doi;
1576
1577 error = dmu_object_info(os, ZVOL_OBJ, doi);
1578 if (error)
1579 goto out_dmu_objset_disown;
1580
1581 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
1582 if (error)
1583 goto out_dmu_objset_disown;
1584
1585 zv = zvol_alloc(MKDEV(zvol_major, minor), name);
1586 if (zv == NULL) {
1587 error = SET_ERROR(EAGAIN);
1588 goto out_dmu_objset_disown;
1589 }
1590 zv->zv_hash = hash;
1591
1592 if (dmu_objset_is_snapshot(os))
1593 zv->zv_flags |= ZVOL_RDONLY;
1594
1595 zv->zv_volblocksize = doi->doi_data_block_size;
1596 zv->zv_volsize = volsize;
1597 zv->zv_objset = os;
1598
1599 set_capacity(zv->zv_disk, zv->zv_volsize >> 9);
1600
1601 blk_queue_max_hw_sectors(zv->zv_queue, (DMU_MAX_ACCESS / 4) >> 9);
1602 blk_queue_max_segments(zv->zv_queue, UINT16_MAX);
1603 blk_queue_max_segment_size(zv->zv_queue, UINT_MAX);
1604 blk_queue_physical_block_size(zv->zv_queue, zv->zv_volblocksize);
1605 blk_queue_io_opt(zv->zv_queue, zv->zv_volblocksize);
1606 blk_queue_max_discard_sectors(zv->zv_queue,
1607 (zvol_max_discard_blocks * zv->zv_volblocksize) >> 9);
1608 blk_queue_discard_granularity(zv->zv_queue, zv->zv_volblocksize);
1609 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, zv->zv_queue);
1610 #ifdef QUEUE_FLAG_NONROT
1611 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zv->zv_queue);
1612 #endif
1613 #ifdef QUEUE_FLAG_ADD_RANDOM
1614 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, zv->zv_queue);
1615 #endif
1616
1617 if (spa_writeable(dmu_objset_spa(os))) {
1618 if (zil_replay_disable)
1619 zil_destroy(dmu_objset_zil(os), B_FALSE);
1620 else
1621 zil_replay(os, zv, zvol_replay_vector);
1622 }
1623
1624 /*
1625 * When udev detects the addition of the device it will immediately
1626 * invoke blkid(8) to determine the type of content on the device.
1627 * Prefetching the blocks commonly scanned by blkid(8) will speed
1628 * up this process.
1629 */
1630 len = MIN(MAX(zvol_prefetch_bytes, 0), SPA_MAXBLOCKSIZE);
1631 if (len > 0) {
1632 dmu_prefetch(os, ZVOL_OBJ, 0, 0, len, ZIO_PRIORITY_SYNC_READ);
1633 dmu_prefetch(os, ZVOL_OBJ, 0, volsize - len, len,
1634 ZIO_PRIORITY_SYNC_READ);
1635 }
1636
1637 zv->zv_objset = NULL;
1638 out_dmu_objset_disown:
1639 dmu_objset_disown(os, FTAG);
1640 out_doi:
1641 kmem_free(doi, sizeof (dmu_object_info_t));
1642 out:
1643
1644 if (error == 0) {
1645 zvol_insert(zv);
1646 /*
1647 * Drop the lock to prevent deadlock with sys_open() ->
1648 * zvol_open(), which first takes bd_disk->bd_mutex and then
1649 * takes zvol_state_lock, whereas this code path first takes
1650 * zvol_state_lock, and then takes bd_disk->bd_mutex.
1651 */
1652 mutex_exit(&zvol_state_lock);
1653 add_disk(zv->zv_disk);
1654 } else {
1655 mutex_exit(&zvol_state_lock);
1656 ida_simple_remove(&zvol_ida, idx);
1657 }
1658
1659 return (SET_ERROR(error));
1660 }
1661
1662 /*
1663 * Rename a block device minor mode for the specified volume.
1664 */
1665 static void
1666 zvol_rename_minor(zvol_state_t *zv, const char *newname)
1667 {
1668 int readonly = get_disk_ro(zv->zv_disk);
1669
1670 ASSERT(MUTEX_HELD(&zvol_state_lock));
1671
1672 rw_enter(&zv->zv_suspend_lock, RW_READER);
1673 strlcpy(zv->zv_name, newname, sizeof (zv->zv_name));
1674 rw_exit(&zv->zv_suspend_lock);
1675
1676 /* move to new hashtable entry */
1677 zv->zv_hash = zvol_name_hash(zv->zv_name);
1678 hlist_del(&zv->zv_hlink);
1679 hlist_add_head(&zv->zv_hlink, ZVOL_HT_HEAD(zv->zv_hash));
1680
1681 /*
1682 * The block device's read-only state is briefly changed causing
1683 * a KOBJ_CHANGE uevent to be issued. This ensures udev detects
1684 * the name change and fixes the symlinks. This does not change
1685 * ZVOL_RDONLY in zv->zv_flags so the actual read-only state never
1686 * changes. This would normally be done using kobject_uevent() but
1687 * that is a GPL-only symbol which is why we need this workaround.
1688 */
1689 set_disk_ro(zv->zv_disk, !readonly);
1690 set_disk_ro(zv->zv_disk, readonly);
1691 }
1692
1693 typedef struct minors_job {
1694 list_t *list;
1695 list_node_t link;
1696 /* input */
1697 char *name;
1698 /* output */
1699 int error;
1700 } minors_job_t;
1701
1702 /*
1703 * Prefetch zvol dnodes for the minors_job
1704 */
1705 static void
1706 zvol_prefetch_minors_impl(void *arg)
1707 {
1708 minors_job_t *job = arg;
1709 char *dsname = job->name;
1710 objset_t *os = NULL;
1711
1712 job->error = dmu_objset_own(dsname, DMU_OST_ZVOL, B_TRUE, FTAG,
1713 &os);
1714 if (job->error == 0) {
1715 dmu_prefetch(os, ZVOL_OBJ, 0, 0, 0, ZIO_PRIORITY_SYNC_READ);
1716 dmu_objset_disown(os, FTAG);
1717 }
1718 }
1719
1720 /*
1721 * Mask errors to continue dmu_objset_find() traversal
1722 */
1723 static int
1724 zvol_create_snap_minor_cb(const char *dsname, void *arg)
1725 {
1726 minors_job_t *j = arg;
1727 list_t *minors_list = j->list;
1728 const char *name = j->name;
1729
1730 ASSERT0(MUTEX_HELD(&spa_namespace_lock));
1731
1732 /* skip the designated dataset */
1733 if (name && strcmp(dsname, name) == 0)
1734 return (0);
1735
1736 /* at this point, the dsname should name a snapshot */
1737 if (strchr(dsname, '@') == 0) {
1738 dprintf("zvol_create_snap_minor_cb(): "
1739 "%s is not a shapshot name\n", dsname);
1740 } else {
1741 minors_job_t *job;
1742 char *n = strdup(dsname);
1743 if (n == NULL)
1744 return (0);
1745
1746 job = kmem_alloc(sizeof (minors_job_t), KM_SLEEP);
1747 job->name = n;
1748 job->list = minors_list;
1749 job->error = 0;
1750 list_insert_tail(minors_list, job);
1751 /* don't care if dispatch fails, because job->error is 0 */
1752 taskq_dispatch(system_taskq, zvol_prefetch_minors_impl, job,
1753 TQ_SLEEP);
1754 }
1755
1756 return (0);
1757 }
1758
1759 /*
1760 * Mask errors to continue dmu_objset_find() traversal
1761 */
1762 static int
1763 zvol_create_minors_cb(const char *dsname, void *arg)
1764 {
1765 uint64_t snapdev;
1766 int error;
1767 list_t *minors_list = arg;
1768
1769 ASSERT0(MUTEX_HELD(&spa_namespace_lock));
1770
1771 error = dsl_prop_get_integer(dsname, "snapdev", &snapdev, NULL);
1772 if (error)
1773 return (0);
1774
1775 /*
1776 * Given the name and the 'snapdev' property, create device minor nodes
1777 * with the linkages to zvols/snapshots as needed.
1778 * If the name represents a zvol, create a minor node for the zvol, then
1779 * check if its snapshots are 'visible', and if so, iterate over the
1780 * snapshots and create device minor nodes for those.
1781 */
1782 if (strchr(dsname, '@') == 0) {
1783 minors_job_t *job;
1784 char *n = strdup(dsname);
1785 if (n == NULL)
1786 return (0);
1787
1788 job = kmem_alloc(sizeof (minors_job_t), KM_SLEEP);
1789 job->name = n;
1790 job->list = minors_list;
1791 job->error = 0;
1792 list_insert_tail(minors_list, job);
1793 /* don't care if dispatch fails, because job->error is 0 */
1794 taskq_dispatch(system_taskq, zvol_prefetch_minors_impl, job,
1795 TQ_SLEEP);
1796
1797 if (snapdev == ZFS_SNAPDEV_VISIBLE) {
1798 /*
1799 * traverse snapshots only, do not traverse children,
1800 * and skip the 'dsname'
1801 */
1802 error = dmu_objset_find((char *)dsname,
1803 zvol_create_snap_minor_cb, (void *)job,
1804 DS_FIND_SNAPSHOTS);
1805 }
1806 } else {
1807 dprintf("zvol_create_minors_cb(): %s is not a zvol name\n",
1808 dsname);
1809 }
1810
1811 return (0);
1812 }
1813
1814 /*
1815 * Create minors for the specified dataset, including children and snapshots.
1816 * Pay attention to the 'snapdev' property and iterate over the snapshots
1817 * only if they are 'visible'. This approach allows one to assure that the
1818 * snapshot metadata is read from disk only if it is needed.
1819 *
1820 * The name can represent a dataset to be recursively scanned for zvols and
1821 * their snapshots, or a single zvol snapshot. If the name represents a
1822 * dataset, the scan is performed in two nested stages:
1823 * - scan the dataset for zvols, and
1824 * - for each zvol, create a minor node, then check if the zvol's snapshots
1825 * are 'visible', and only then iterate over the snapshots if needed
1826 *
1827 * If the name represents a snapshot, a check is performed if the snapshot is
1828 * 'visible' (which also verifies that the parent is a zvol), and if so,
1829 * a minor node for that snapshot is created.
1830 */
1831 static int
1832 zvol_create_minors_impl(const char *name)
1833 {
1834 int error = 0;
1835 fstrans_cookie_t cookie;
1836 char *atp, *parent;
1837 list_t minors_list;
1838 minors_job_t *job;
1839
1840 if (zvol_inhibit_dev)
1841 return (0);
1842
1843 /*
1844 * This is the list for prefetch jobs. Whenever we found a match
1845 * during dmu_objset_find, we insert a minors_job to the list and do
1846 * taskq_dispatch to parallel prefetch zvol dnodes. Note we don't need
1847 * any lock because all list operation is done on the current thread.
1848 *
1849 * We will use this list to do zvol_create_minor_impl after prefetch
1850 * so we don't have to traverse using dmu_objset_find again.
1851 */
1852 list_create(&minors_list, sizeof (minors_job_t),
1853 offsetof(minors_job_t, link));
1854
1855 parent = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1856 (void) strlcpy(parent, name, MAXPATHLEN);
1857
1858 if ((atp = strrchr(parent, '@')) != NULL) {
1859 uint64_t snapdev;
1860
1861 *atp = '\0';
1862 error = dsl_prop_get_integer(parent, "snapdev",
1863 &snapdev, NULL);
1864
1865 if (error == 0 && snapdev == ZFS_SNAPDEV_VISIBLE)
1866 error = zvol_create_minor_impl(name);
1867 } else {
1868 cookie = spl_fstrans_mark();
1869 error = dmu_objset_find(parent, zvol_create_minors_cb,
1870 &minors_list, DS_FIND_CHILDREN);
1871 spl_fstrans_unmark(cookie);
1872 }
1873
1874 kmem_free(parent, MAXPATHLEN);
1875 taskq_wait_outstanding(system_taskq, 0);
1876
1877 /*
1878 * Prefetch is completed, we can do zvol_create_minor_impl
1879 * sequentially.
1880 */
1881 while ((job = list_head(&minors_list)) != NULL) {
1882 list_remove(&minors_list, job);
1883 if (!job->error)
1884 zvol_create_minor_impl(job->name);
1885 strfree(job->name);
1886 kmem_free(job, sizeof (minors_job_t));
1887 }
1888
1889 list_destroy(&minors_list);
1890
1891 return (SET_ERROR(error));
1892 }
1893
1894 /*
1895 * Remove minors for specified dataset including children and snapshots.
1896 */
1897 static void
1898 zvol_remove_minors_impl(const char *name)
1899 {
1900 zvol_state_t *zv, *zv_next;
1901 int namelen = ((name) ? strlen(name) : 0);
1902 taskqid_t t, tid = TASKQID_INVALID;
1903
1904 if (zvol_inhibit_dev)
1905 return;
1906
1907 mutex_enter(&zvol_state_lock);
1908
1909 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1910 zv_next = list_next(&zvol_state_list, zv);
1911
1912 if (name == NULL || strcmp(zv->zv_name, name) == 0 ||
1913 (strncmp(zv->zv_name, name, namelen) == 0 &&
1914 (zv->zv_name[namelen] == '/' ||
1915 zv->zv_name[namelen] == '@'))) {
1916
1917 /* If in use, leave alone */
1918 if (zv->zv_open_count > 0 ||
1919 atomic_read(&zv->zv_suspend_ref))
1920 continue;
1921
1922 zvol_remove(zv);
1923
1924 /* clear this so zvol_open won't open it */
1925 zv->zv_disk->private_data = NULL;
1926
1927 /* try parallel zv_free, if failed do it in place */
1928 t = taskq_dispatch(system_taskq, zvol_free_impl, zv,
1929 TQ_SLEEP);
1930 if (t == TASKQID_INVALID)
1931 zvol_free(zv);
1932 else
1933 tid = t;
1934 }
1935 }
1936 mutex_exit(&zvol_state_lock);
1937 if (tid != TASKQID_INVALID)
1938 taskq_wait_outstanding(system_taskq, tid);
1939 }
1940
1941 /* Remove minor for this specific snapshot only */
1942 static void
1943 zvol_remove_minor_impl(const char *name)
1944 {
1945 zvol_state_t *zv, *zv_next;
1946
1947 if (zvol_inhibit_dev)
1948 return;
1949
1950 if (strchr(name, '@') == NULL)
1951 return;
1952
1953 mutex_enter(&zvol_state_lock);
1954
1955 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1956 zv_next = list_next(&zvol_state_list, zv);
1957
1958 if (strcmp(zv->zv_name, name) == 0) {
1959 /* If in use, leave alone */
1960 if (zv->zv_open_count > 0 ||
1961 atomic_read(&zv->zv_suspend_ref))
1962 continue;
1963 zvol_remove(zv);
1964 zvol_free(zv);
1965 break;
1966 }
1967 }
1968
1969 mutex_exit(&zvol_state_lock);
1970 }
1971
1972 /*
1973 * Rename minors for specified dataset including children and snapshots.
1974 */
1975 static void
1976 zvol_rename_minors_impl(const char *oldname, const char *newname)
1977 {
1978 zvol_state_t *zv, *zv_next;
1979 int oldnamelen, newnamelen;
1980 char *name;
1981
1982 if (zvol_inhibit_dev)
1983 return;
1984
1985 oldnamelen = strlen(oldname);
1986 newnamelen = strlen(newname);
1987 name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1988
1989 mutex_enter(&zvol_state_lock);
1990
1991 for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) {
1992 zv_next = list_next(&zvol_state_list, zv);
1993
1994 /* If in use, leave alone */
1995 if (zv->zv_open_count > 0)
1996 continue;
1997
1998 if (strcmp(zv->zv_name, oldname) == 0) {
1999 zvol_rename_minor(zv, newname);
2000 } else if (strncmp(zv->zv_name, oldname, oldnamelen) == 0 &&
2001 (zv->zv_name[oldnamelen] == '/' ||
2002 zv->zv_name[oldnamelen] == '@')) {
2003 snprintf(name, MAXNAMELEN, "%s%c%s", newname,
2004 zv->zv_name[oldnamelen],
2005 zv->zv_name + oldnamelen + 1);
2006 zvol_rename_minor(zv, name);
2007 }
2008 }
2009
2010 mutex_exit(&zvol_state_lock);
2011
2012 kmem_free(name, MAXNAMELEN);
2013 }
2014
2015 typedef struct zvol_snapdev_cb_arg {
2016 uint64_t snapdev;
2017 } zvol_snapdev_cb_arg_t;
2018
2019 static int
2020 zvol_set_snapdev_cb(const char *dsname, void *param)
2021 {
2022 zvol_snapdev_cb_arg_t *arg = param;
2023
2024 if (strchr(dsname, '@') == NULL)
2025 return (0);
2026
2027 switch (arg->snapdev) {
2028 case ZFS_SNAPDEV_VISIBLE:
2029 (void) zvol_create_minor_impl(dsname);
2030 break;
2031 case ZFS_SNAPDEV_HIDDEN:
2032 (void) zvol_remove_minor_impl(dsname);
2033 break;
2034 }
2035
2036 return (0);
2037 }
2038
2039 static void
2040 zvol_set_snapdev_impl(char *name, uint64_t snapdev)
2041 {
2042 zvol_snapdev_cb_arg_t arg = {snapdev};
2043 fstrans_cookie_t cookie = spl_fstrans_mark();
2044 /*
2045 * The zvol_set_snapdev_sync() sets snapdev appropriately
2046 * in the dataset hierarchy. Here, we only scan snapshots.
2047 */
2048 dmu_objset_find(name, zvol_set_snapdev_cb, &arg, DS_FIND_SNAPSHOTS);
2049 spl_fstrans_unmark(cookie);
2050 }
2051
2052 static zvol_task_t *
2053 zvol_task_alloc(zvol_async_op_t op, const char *name1, const char *name2,
2054 uint64_t snapdev)
2055 {
2056 zvol_task_t *task;
2057 char *delim;
2058
2059 /* Never allow tasks on hidden names. */
2060 if (name1[0] == '$')
2061 return (NULL);
2062
2063 task = kmem_zalloc(sizeof (zvol_task_t), KM_SLEEP);
2064 task->op = op;
2065 task->snapdev = snapdev;
2066 delim = strchr(name1, '/');
2067 strlcpy(task->pool, name1, delim ? (delim - name1 + 1) : MAXNAMELEN);
2068
2069 strlcpy(task->name1, name1, MAXNAMELEN);
2070 if (name2 != NULL)
2071 strlcpy(task->name2, name2, MAXNAMELEN);
2072
2073 return (task);
2074 }
2075
2076 static void
2077 zvol_task_free(zvol_task_t *task)
2078 {
2079 kmem_free(task, sizeof (zvol_task_t));
2080 }
2081
2082 /*
2083 * The worker thread function performed asynchronously.
2084 */
2085 static void
2086 zvol_task_cb(void *param)
2087 {
2088 zvol_task_t *task = (zvol_task_t *)param;
2089
2090 switch (task->op) {
2091 case ZVOL_ASYNC_CREATE_MINORS:
2092 (void) zvol_create_minors_impl(task->name1);
2093 break;
2094 case ZVOL_ASYNC_REMOVE_MINORS:
2095 zvol_remove_minors_impl(task->name1);
2096 break;
2097 case ZVOL_ASYNC_RENAME_MINORS:
2098 zvol_rename_minors_impl(task->name1, task->name2);
2099 break;
2100 case ZVOL_ASYNC_SET_SNAPDEV:
2101 zvol_set_snapdev_impl(task->name1, task->snapdev);
2102 break;
2103 default:
2104 VERIFY(0);
2105 break;
2106 }
2107
2108 zvol_task_free(task);
2109 }
2110
2111 typedef struct zvol_set_snapdev_arg {
2112 const char *zsda_name;
2113 uint64_t zsda_value;
2114 zprop_source_t zsda_source;
2115 dmu_tx_t *zsda_tx;
2116 } zvol_set_snapdev_arg_t;
2117
2118 /*
2119 * Sanity check the dataset for safe use by the sync task. No additional
2120 * conditions are imposed.
2121 */
2122 static int
2123 zvol_set_snapdev_check(void *arg, dmu_tx_t *tx)
2124 {
2125 zvol_set_snapdev_arg_t *zsda = arg;
2126 dsl_pool_t *dp = dmu_tx_pool(tx);
2127 dsl_dir_t *dd;
2128 int error;
2129
2130 error = dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL);
2131 if (error != 0)
2132 return (error);
2133
2134 dsl_dir_rele(dd, FTAG);
2135
2136 return (error);
2137 }
2138
2139 static int
2140 zvol_set_snapdev_sync_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
2141 {
2142 zvol_set_snapdev_arg_t *zsda = arg;
2143 char dsname[MAXNAMELEN];
2144 zvol_task_t *task;
2145
2146 dsl_dataset_name(ds, dsname);
2147 dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_SNAPDEV),
2148 zsda->zsda_source, sizeof (zsda->zsda_value), 1,
2149 &zsda->zsda_value, zsda->zsda_tx);
2150
2151 task = zvol_task_alloc(ZVOL_ASYNC_SET_SNAPDEV, dsname,
2152 NULL, zsda->zsda_value);
2153 if (task == NULL)
2154 return (0);
2155
2156 (void) taskq_dispatch(dp->dp_spa->spa_zvol_taskq, zvol_task_cb,
2157 task, TQ_SLEEP);
2158 return (0);
2159 }
2160
2161 /*
2162 * Traverse all child snapshot datasets and apply snapdev appropriately.
2163 */
2164 static void
2165 zvol_set_snapdev_sync(void *arg, dmu_tx_t *tx)
2166 {
2167 zvol_set_snapdev_arg_t *zsda = arg;
2168 dsl_pool_t *dp = dmu_tx_pool(tx);
2169 dsl_dir_t *dd;
2170
2171 VERIFY0(dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL));
2172 zsda->zsda_tx = tx;
2173
2174 dmu_objset_find_dp(dp, dd->dd_object, zvol_set_snapdev_sync_cb,
2175 zsda, DS_FIND_CHILDREN);
2176
2177 dsl_dir_rele(dd, FTAG);
2178 }
2179
2180 int
2181 zvol_set_snapdev(const char *ddname, zprop_source_t source, uint64_t snapdev)
2182 {
2183 zvol_set_snapdev_arg_t zsda;
2184
2185 zsda.zsda_name = ddname;
2186 zsda.zsda_source = source;
2187 zsda.zsda_value = snapdev;
2188
2189 return (dsl_sync_task(ddname, zvol_set_snapdev_check,
2190 zvol_set_snapdev_sync, &zsda, 0, ZFS_SPACE_CHECK_NONE));
2191 }
2192
2193 void
2194 zvol_create_minors(spa_t *spa, const char *name, boolean_t async)
2195 {
2196 zvol_task_t *task;
2197 taskqid_t id;
2198
2199 task = zvol_task_alloc(ZVOL_ASYNC_CREATE_MINORS, name, NULL, ~0ULL);
2200 if (task == NULL)
2201 return;
2202
2203 id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP);
2204 if ((async == B_FALSE) && (id != TASKQID_INVALID))
2205 taskq_wait_id(spa->spa_zvol_taskq, id);
2206 }
2207
2208 void
2209 zvol_remove_minors(spa_t *spa, const char *name, boolean_t async)
2210 {
2211 zvol_task_t *task;
2212 taskqid_t id;
2213
2214 task = zvol_task_alloc(ZVOL_ASYNC_REMOVE_MINORS, name, NULL, ~0ULL);
2215 if (task == NULL)
2216 return;
2217
2218 id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP);
2219 if ((async == B_FALSE) && (id != TASKQID_INVALID))
2220 taskq_wait_id(spa->spa_zvol_taskq, id);
2221 }
2222
2223 void
2224 zvol_rename_minors(spa_t *spa, const char *name1, const char *name2,
2225 boolean_t async)
2226 {
2227 zvol_task_t *task;
2228 taskqid_t id;
2229
2230 task = zvol_task_alloc(ZVOL_ASYNC_RENAME_MINORS, name1, name2, ~0ULL);
2231 if (task == NULL)
2232 return;
2233
2234 id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP);
2235 if ((async == B_FALSE) && (id != TASKQID_INVALID))
2236 taskq_wait_id(spa->spa_zvol_taskq, id);
2237 }
2238
2239 int
2240 zvol_init(void)
2241 {
2242 int threads = MIN(MAX(zvol_threads, 1), 1024);
2243 int i, error;
2244
2245 list_create(&zvol_state_list, sizeof (zvol_state_t),
2246 offsetof(zvol_state_t, zv_next));
2247 mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL);
2248 ida_init(&zvol_ida);
2249
2250 zvol_taskq = taskq_create(ZVOL_DRIVER, threads, maxclsyspri,
2251 threads * 2, INT_MAX, TASKQ_PREPOPULATE | TASKQ_DYNAMIC);
2252 if (zvol_taskq == NULL) {
2253 printk(KERN_INFO "ZFS: taskq_create() failed\n");
2254 error = -ENOMEM;
2255 goto out;
2256 }
2257
2258 zvol_htable = kmem_alloc(ZVOL_HT_SIZE * sizeof (struct hlist_head),
2259 KM_SLEEP);
2260 if (!zvol_htable) {
2261 error = -ENOMEM;
2262 goto out_taskq;
2263 }
2264 for (i = 0; i < ZVOL_HT_SIZE; i++)
2265 INIT_HLIST_HEAD(&zvol_htable[i]);
2266
2267 error = register_blkdev(zvol_major, ZVOL_DRIVER);
2268 if (error) {
2269 printk(KERN_INFO "ZFS: register_blkdev() failed %d\n", error);
2270 goto out_free;
2271 }
2272
2273 blk_register_region(MKDEV(zvol_major, 0), 1UL << MINORBITS,
2274 THIS_MODULE, zvol_probe, NULL, NULL);
2275
2276 return (0);
2277
2278 out_free:
2279 kmem_free(zvol_htable, ZVOL_HT_SIZE * sizeof (struct hlist_head));
2280 out_taskq:
2281 taskq_destroy(zvol_taskq);
2282 out:
2283 mutex_destroy(&zvol_state_lock);
2284 list_destroy(&zvol_state_list);
2285
2286 return (SET_ERROR(error));
2287 }
2288
2289 void
2290 zvol_fini(void)
2291 {
2292 zvol_remove_minors_impl(NULL);
2293
2294 blk_unregister_region(MKDEV(zvol_major, 0), 1UL << MINORBITS);
2295 unregister_blkdev(zvol_major, ZVOL_DRIVER);
2296 kmem_free(zvol_htable, ZVOL_HT_SIZE * sizeof (struct hlist_head));
2297
2298 taskq_destroy(zvol_taskq);
2299 list_destroy(&zvol_state_list);
2300 mutex_destroy(&zvol_state_lock);
2301
2302 ida_destroy(&zvol_ida);
2303 }
2304
2305 /* BEGIN CSTYLED */
2306 module_param(zvol_inhibit_dev, uint, 0644);
2307 MODULE_PARM_DESC(zvol_inhibit_dev, "Do not create zvol device nodes");
2308
2309 module_param(zvol_major, uint, 0444);
2310 MODULE_PARM_DESC(zvol_major, "Major number for zvol device");
2311
2312 module_param(zvol_threads, uint, 0444);
2313 MODULE_PARM_DESC(zvol_threads, "Max number of threads to handle I/O requests");
2314
2315 module_param(zvol_request_sync, uint, 0644);
2316 MODULE_PARM_DESC(zvol_request_sync, "Synchronously handle bio requests");
2317
2318 module_param(zvol_max_discard_blocks, ulong, 0444);
2319 MODULE_PARM_DESC(zvol_max_discard_blocks, "Max number of blocks to discard");
2320
2321 module_param(zvol_prefetch_bytes, uint, 0644);
2322 MODULE_PARM_DESC(zvol_prefetch_bytes, "Prefetch N bytes at zvol start+end");
2323 /* END CSTYLED */