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