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