]>
Commit | Line | Data |
---|---|---|
60101509 BB |
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. | |
460a0213 DM |
36 | * |
37 | * Copyright 2014 Nexenta Systems, Inc. All rights reserved. | |
5428dc51 | 38 | * Copyright (c) 2016 Actifio, Inc. All rights reserved. |
1eacf2b3 | 39 | * Copyright (c) 2012, 2019 by Delphix. All rights reserved. |
60101509 BB |
40 | */ |
41 | ||
5559ba09 BP |
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 | * | |
58404a73 BP |
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 | |
5559ba09 BP |
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 | * | |
5559ba09 BP |
71 | */ |
72 | ||
a448a255 | 73 | #include <sys/dataset_kstats.h> |
03c6040b | 74 | #include <sys/dbuf.h> |
60101509 BB |
75 | #include <sys/dmu_traverse.h> |
76 | #include <sys/dsl_dataset.h> | |
77 | #include <sys/dsl_prop.h> | |
a0bd735a | 78 | #include <sys/dsl_dir.h> |
60101509 | 79 | #include <sys/zap.h> |
4cb7b9c5 | 80 | #include <sys/zfeature.h> |
60101509 | 81 | #include <sys/zil_impl.h> |
460a0213 | 82 | #include <sys/dmu_tx.h> |
60101509 BB |
83 | #include <sys/zio.h> |
84 | #include <sys/zfs_rlock.h> | |
a0bd735a | 85 | #include <sys/spa_impl.h> |
60101509 | 86 | #include <sys/zvol.h> |
a448a255 | 87 | |
5df7e9d8 MM |
88 | #include <sys/zvol_impl.h> |
89 | ||
60101509 | 90 | |
74497b7a | 91 | unsigned int zvol_inhibit_dev = 0; |
cf8738d8 | 92 | unsigned int zvol_volmode = ZFS_VOLMODE_GEOM; |
60101509 | 93 | |
5df7e9d8 MM |
94 | struct hlist_head *zvol_htable; |
95 | list_t zvol_state_list; | |
96 | krwlock_t zvol_state_lock; | |
97 | const zvol_platform_ops_t *ops; | |
60101509 | 98 | |
a0bd735a BP |
99 | typedef enum { |
100 | ZVOL_ASYNC_CREATE_MINORS, | |
101 | ZVOL_ASYNC_REMOVE_MINORS, | |
102 | ZVOL_ASYNC_RENAME_MINORS, | |
103 | ZVOL_ASYNC_SET_SNAPDEV, | |
cf8738d8 | 104 | ZVOL_ASYNC_SET_VOLMODE, |
a0bd735a BP |
105 | ZVOL_ASYNC_MAX |
106 | } zvol_async_op_t; | |
107 | ||
108 | typedef struct { | |
109 | zvol_async_op_t op; | |
110 | char pool[MAXNAMELEN]; | |
111 | char name1[MAXNAMELEN]; | |
112 | char name2[MAXNAMELEN]; | |
113 | zprop_source_t source; | |
cf8738d8 | 114 | uint64_t value; |
a0bd735a BP |
115 | } zvol_task_t; |
116 | ||
5df7e9d8 | 117 | uint64_t |
d45e010d | 118 | zvol_name_hash(const char *name) |
60101509 | 119 | { |
d45e010d CC |
120 | int i; |
121 | uint64_t crc = -1ULL; | |
5df7e9d8 | 122 | const uint8_t *p = (const uint8_t *)name; |
d45e010d CC |
123 | ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); |
124 | for (i = 0; i < MAXNAMELEN - 1 && *p; i++, p++) { | |
125 | crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (*p)) & 0xFF]; | |
60101509 | 126 | } |
d45e010d | 127 | return (crc); |
60101509 BB |
128 | } |
129 | ||
60101509 | 130 | /* |
d45e010d | 131 | * Find a zvol_state_t given the name and hash generated by zvol_name_hash. |
58404a73 BP |
132 | * If found, return with zv_suspend_lock and zv_state_lock taken, otherwise, |
133 | * return (NULL) without the taking locks. The zv_suspend_lock is always taken | |
134 | * before zv_state_lock. The mode argument indicates the mode (including none) | |
135 | * for zv_suspend_lock to be taken. | |
60101509 | 136 | */ |
5df7e9d8 | 137 | zvol_state_t * |
58404a73 | 138 | zvol_find_by_name_hash(const char *name, uint64_t hash, int mode) |
60101509 BB |
139 | { |
140 | zvol_state_t *zv; | |
94183a9d | 141 | struct hlist_node *p = NULL; |
60101509 | 142 | |
7b98f0d9 | 143 | rw_enter(&zvol_state_lock, RW_READER); |
d45e010d CC |
144 | hlist_for_each(p, ZVOL_HT_HEAD(hash)) { |
145 | zv = hlist_entry(p, zvol_state_t, zv_hlink); | |
58404a73 | 146 | mutex_enter(&zv->zv_state_lock); |
d45e010d | 147 | if (zv->zv_hash == hash && |
58404a73 BP |
148 | strncmp(zv->zv_name, name, MAXNAMELEN) == 0) { |
149 | /* | |
150 | * this is the right zvol, take the locks in the | |
151 | * right order | |
152 | */ | |
153 | if (mode != RW_NONE && | |
154 | !rw_tryenter(&zv->zv_suspend_lock, mode)) { | |
155 | mutex_exit(&zv->zv_state_lock); | |
156 | rw_enter(&zv->zv_suspend_lock, mode); | |
157 | mutex_enter(&zv->zv_state_lock); | |
158 | /* | |
159 | * zvol cannot be renamed as we continue | |
160 | * to hold zvol_state_lock | |
161 | */ | |
162 | ASSERT(zv->zv_hash == hash && | |
163 | strncmp(zv->zv_name, name, MAXNAMELEN) | |
164 | == 0); | |
165 | } | |
7b98f0d9 | 166 | rw_exit(&zvol_state_lock); |
ce37ebd2 | 167 | return (zv); |
58404a73 BP |
168 | } |
169 | mutex_exit(&zv->zv_state_lock); | |
60101509 | 170 | } |
7b98f0d9 | 171 | rw_exit(&zvol_state_lock); |
58404a73 | 172 | |
ce37ebd2 | 173 | return (NULL); |
60101509 BB |
174 | } |
175 | ||
d45e010d | 176 | /* |
58404a73 BP |
177 | * Find a zvol_state_t given the name. |
178 | * If found, return with zv_suspend_lock and zv_state_lock taken, otherwise, | |
179 | * return (NULL) without the taking locks. The zv_suspend_lock is always taken | |
180 | * before zv_state_lock. The mode argument indicates the mode (including none) | |
181 | * for zv_suspend_lock to be taken. | |
d45e010d CC |
182 | */ |
183 | static zvol_state_t * | |
58404a73 | 184 | zvol_find_by_name(const char *name, int mode) |
d45e010d | 185 | { |
58404a73 | 186 | return (zvol_find_by_name_hash(name, zvol_name_hash(name), mode)); |
d45e010d CC |
187 | } |
188 | ||
60101509 BB |
189 | /* |
190 | * ZFS_IOC_CREATE callback handles dmu zvol and zap object creation. | |
191 | */ | |
192 | void | |
193 | zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) | |
194 | { | |
195 | zfs_creat_t *zct = arg; | |
196 | nvlist_t *nvprops = zct->zct_props; | |
197 | int error; | |
198 | uint64_t volblocksize, volsize; | |
199 | ||
200 | VERIFY(nvlist_lookup_uint64(nvprops, | |
201 | zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0); | |
202 | if (nvlist_lookup_uint64(nvprops, | |
203 | zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0) | |
204 | volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); | |
205 | ||
206 | /* | |
207 | * These properties must be removed from the list so the generic | |
208 | * property setting step won't apply to them. | |
209 | */ | |
210 | VERIFY(nvlist_remove_all(nvprops, | |
211 | zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0); | |
212 | (void) nvlist_remove_all(nvprops, | |
213 | zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE)); | |
214 | ||
215 | error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize, | |
216 | DMU_OT_NONE, 0, tx); | |
217 | ASSERT(error == 0); | |
218 | ||
219 | error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP, | |
220 | DMU_OT_NONE, 0, tx); | |
221 | ASSERT(error == 0); | |
222 | ||
223 | error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx); | |
224 | ASSERT(error == 0); | |
225 | } | |
226 | ||
227 | /* | |
228 | * ZFS_IOC_OBJSET_STATS entry point. | |
229 | */ | |
230 | int | |
231 | zvol_get_stats(objset_t *os, nvlist_t *nv) | |
232 | { | |
233 | int error; | |
234 | dmu_object_info_t *doi; | |
235 | uint64_t val; | |
236 | ||
237 | error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val); | |
238 | if (error) | |
ce37ebd2 | 239 | return (SET_ERROR(error)); |
60101509 BB |
240 | |
241 | dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val); | |
ce37ebd2 | 242 | doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP); |
60101509 BB |
243 | error = dmu_object_info(os, ZVOL_OBJ, doi); |
244 | ||
245 | if (error == 0) { | |
246 | dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE, | |
247 | doi->doi_data_block_size); | |
248 | } | |
249 | ||
ce37ebd2 | 250 | kmem_free(doi, sizeof (dmu_object_info_t)); |
60101509 | 251 | |
ce37ebd2 | 252 | return (SET_ERROR(error)); |
60101509 BB |
253 | } |
254 | ||
255 | /* | |
256 | * Sanity check volume size. | |
257 | */ | |
258 | int | |
259 | zvol_check_volsize(uint64_t volsize, uint64_t blocksize) | |
260 | { | |
261 | if (volsize == 0) | |
2e528b49 | 262 | return (SET_ERROR(EINVAL)); |
60101509 BB |
263 | |
264 | if (volsize % blocksize != 0) | |
2e528b49 | 265 | return (SET_ERROR(EINVAL)); |
60101509 BB |
266 | |
267 | #ifdef _ILP32 | |
82ec9d41 | 268 | if (volsize - 1 > SPEC_MAXOFFSET_T) |
2e528b49 | 269 | return (SET_ERROR(EOVERFLOW)); |
60101509 BB |
270 | #endif |
271 | return (0); | |
272 | } | |
273 | ||
274 | /* | |
275 | * Ensure the zap is flushed then inform the VFS of the capacity change. | |
276 | */ | |
277 | static int | |
35d3e322 | 278 | zvol_update_volsize(uint64_t volsize, objset_t *os) |
60101509 | 279 | { |
60101509 BB |
280 | dmu_tx_t *tx; |
281 | int error; | |
513168ab | 282 | uint64_t txg; |
60101509 | 283 | |
df554c14 | 284 | tx = dmu_tx_create(os); |
60101509 | 285 | dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); |
19d55079 | 286 | dmu_tx_mark_netfree(tx); |
60101509 BB |
287 | error = dmu_tx_assign(tx, TXG_WAIT); |
288 | if (error) { | |
289 | dmu_tx_abort(tx); | |
ce37ebd2 | 290 | return (SET_ERROR(error)); |
60101509 | 291 | } |
513168ab | 292 | txg = dmu_tx_get_txg(tx); |
60101509 | 293 | |
df554c14 | 294 | error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, |
60101509 BB |
295 | &volsize, tx); |
296 | dmu_tx_commit(tx); | |
297 | ||
513168ab | 298 | txg_wait_synced(dmu_objset_pool(os), txg); |
299 | ||
35d3e322 BB |
300 | if (error == 0) |
301 | error = dmu_free_long_range(os, | |
302 | ZVOL_OBJ, volsize, DMU_OBJECT_END); | |
60101509 | 303 | |
35d3e322 BB |
304 | return (error); |
305 | } | |
60101509 | 306 | |
60101509 | 307 | /* |
7b98f0d9 BB |
308 | * Set ZFS_PROP_VOLSIZE set entry point. Note that modifying the volume |
309 | * size will result in a udev "change" event being generated. | |
60101509 BB |
310 | */ |
311 | int | |
312 | zvol_set_volsize(const char *name, uint64_t volsize) | |
313 | { | |
60101509 | 314 | objset_t *os = NULL; |
35d3e322 | 315 | uint64_t readonly; |
7b98f0d9 | 316 | int error; |
35d3e322 | 317 | boolean_t owned = B_FALSE; |
60101509 | 318 | |
13fe0198 MA |
319 | error = dsl_prop_get_integer(name, |
320 | zfs_prop_to_name(ZFS_PROP_READONLY), &readonly, NULL); | |
321 | if (error != 0) | |
ce37ebd2 | 322 | return (SET_ERROR(error)); |
13fe0198 | 323 | if (readonly) |
2e528b49 | 324 | return (SET_ERROR(EROFS)); |
13fe0198 | 325 | |
7b98f0d9 | 326 | zvol_state_t *zv = zvol_find_by_name(name, RW_READER); |
58404a73 BP |
327 | |
328 | ASSERT(zv == NULL || (MUTEX_HELD(&zv->zv_state_lock) && | |
329 | RW_READ_HELD(&zv->zv_suspend_lock))); | |
35d3e322 BB |
330 | |
331 | if (zv == NULL || zv->zv_objset == NULL) { | |
58404a73 BP |
332 | if (zv != NULL) |
333 | rw_exit(&zv->zv_suspend_lock); | |
b5256303 | 334 | if ((error = dmu_objset_own(name, DMU_OST_ZVOL, B_FALSE, B_TRUE, |
35d3e322 | 335 | FTAG, &os)) != 0) { |
5559ba09 BP |
336 | if (zv != NULL) |
337 | mutex_exit(&zv->zv_state_lock); | |
35d3e322 BB |
338 | return (SET_ERROR(error)); |
339 | } | |
340 | owned = B_TRUE; | |
341 | if (zv != NULL) | |
342 | zv->zv_objset = os; | |
343 | } else { | |
344 | os = zv->zv_objset; | |
60101509 BB |
345 | } |
346 | ||
7b98f0d9 | 347 | dmu_object_info_t *doi = kmem_alloc(sizeof (*doi), KM_SLEEP); |
60101509 | 348 | |
ce37ebd2 BB |
349 | if ((error = dmu_object_info(os, ZVOL_OBJ, doi)) || |
350 | (error = zvol_check_volsize(volsize, doi->doi_data_block_size))) | |
35d3e322 | 351 | goto out; |
60101509 | 352 | |
35d3e322 | 353 | error = zvol_update_volsize(volsize, os); |
7b98f0d9 BB |
354 | if (error == 0 && zv != NULL) { |
355 | zv->zv_volsize = volsize; | |
356 | zv->zv_changed = 1; | |
7b98f0d9 | 357 | } |
35d3e322 | 358 | out: |
3f7d0418 | 359 | kmem_free(doi, sizeof (dmu_object_info_t)); |
360 | ||
35d3e322 | 361 | if (owned) { |
b5256303 | 362 | dmu_objset_disown(os, B_TRUE, FTAG); |
35d3e322 BB |
363 | if (zv != NULL) |
364 | zv->zv_objset = NULL; | |
040dab99 CC |
365 | } else { |
366 | rw_exit(&zv->zv_suspend_lock); | |
35d3e322 | 367 | } |
5559ba09 BP |
368 | |
369 | if (zv != NULL) | |
370 | mutex_exit(&zv->zv_state_lock); | |
58404a73 | 371 | |
5df7e9d8 MM |
372 | if (error == 0 && zv != NULL) |
373 | ops->zv_update_volsize(zv, volsize); | |
7b98f0d9 | 374 | |
5559ba09 | 375 | return (SET_ERROR(error)); |
60101509 BB |
376 | } |
377 | ||
378 | /* | |
379 | * Sanity check volume block size. | |
380 | */ | |
381 | int | |
4cb7b9c5 | 382 | zvol_check_volblocksize(const char *name, uint64_t volblocksize) |
60101509 | 383 | { |
4cb7b9c5 BB |
384 | /* Record sizes above 128k need the feature to be enabled */ |
385 | if (volblocksize > SPA_OLD_MAXBLOCKSIZE) { | |
386 | spa_t *spa; | |
387 | int error; | |
388 | ||
389 | if ((error = spa_open(name, &spa, FTAG)) != 0) | |
390 | return (error); | |
391 | ||
392 | if (!spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) { | |
393 | spa_close(spa, FTAG); | |
394 | return (SET_ERROR(ENOTSUP)); | |
395 | } | |
396 | ||
397 | /* | |
398 | * We don't allow setting the property above 1MB, | |
399 | * unless the tunable has been changed. | |
400 | */ | |
401 | if (volblocksize > zfs_max_recordsize) | |
402 | return (SET_ERROR(EDOM)); | |
403 | ||
404 | spa_close(spa, FTAG); | |
405 | } | |
406 | ||
60101509 BB |
407 | if (volblocksize < SPA_MINBLOCKSIZE || |
408 | volblocksize > SPA_MAXBLOCKSIZE || | |
409 | !ISP2(volblocksize)) | |
2e528b49 | 410 | return (SET_ERROR(EDOM)); |
60101509 BB |
411 | |
412 | return (0); | |
413 | } | |
414 | ||
415 | /* | |
416 | * Set ZFS_PROP_VOLBLOCKSIZE set entry point. | |
417 | */ | |
418 | int | |
419 | zvol_set_volblocksize(const char *name, uint64_t volblocksize) | |
420 | { | |
421 | zvol_state_t *zv; | |
422 | dmu_tx_t *tx; | |
423 | int error; | |
424 | ||
58404a73 | 425 | zv = zvol_find_by_name(name, RW_READER); |
60101509 | 426 | |
58404a73 | 427 | if (zv == NULL) |
5559ba09 | 428 | return (SET_ERROR(ENXIO)); |
58404a73 | 429 | |
7b98f0d9 BB |
430 | ASSERT(MUTEX_HELD(&zv->zv_state_lock)); |
431 | ASSERT(RW_READ_HELD(&zv->zv_suspend_lock)); | |
60101509 | 432 | |
ba6a2402 | 433 | if (zv->zv_flags & ZVOL_RDONLY) { |
5559ba09 | 434 | mutex_exit(&zv->zv_state_lock); |
58404a73 | 435 | rw_exit(&zv->zv_suspend_lock); |
5559ba09 | 436 | return (SET_ERROR(EROFS)); |
60101509 BB |
437 | } |
438 | ||
439 | tx = dmu_tx_create(zv->zv_objset); | |
440 | dmu_tx_hold_bonus(tx, ZVOL_OBJ); | |
441 | error = dmu_tx_assign(tx, TXG_WAIT); | |
442 | if (error) { | |
443 | dmu_tx_abort(tx); | |
444 | } else { | |
445 | error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ, | |
446 | volblocksize, 0, tx); | |
447 | if (error == ENOTSUP) | |
2e528b49 | 448 | error = SET_ERROR(EBUSY); |
60101509 BB |
449 | dmu_tx_commit(tx); |
450 | if (error == 0) | |
451 | zv->zv_volblocksize = volblocksize; | |
452 | } | |
5559ba09 BP |
453 | |
454 | mutex_exit(&zv->zv_state_lock); | |
58404a73 | 455 | rw_exit(&zv->zv_suspend_lock); |
60101509 | 456 | |
ce37ebd2 | 457 | return (SET_ERROR(error)); |
60101509 BB |
458 | } |
459 | ||
460a0213 DM |
460 | /* |
461 | * Replay a TX_TRUNCATE ZIL transaction if asked. TX_TRUNCATE is how we | |
462 | * implement DKIOCFREE/free-long-range. | |
463 | */ | |
464 | static int | |
867959b5 | 465 | zvol_replay_truncate(void *arg1, void *arg2, boolean_t byteswap) |
460a0213 | 466 | { |
867959b5 BB |
467 | zvol_state_t *zv = arg1; |
468 | lr_truncate_t *lr = arg2; | |
460a0213 DM |
469 | uint64_t offset, length; |
470 | ||
471 | if (byteswap) | |
472 | byteswap_uint64_array(lr, sizeof (*lr)); | |
473 | ||
474 | offset = lr->lr_offset; | |
475 | length = lr->lr_length; | |
476 | ||
477 | return (dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, offset, length)); | |
478 | } | |
479 | ||
60101509 BB |
480 | /* |
481 | * Replay a TX_WRITE ZIL transaction that didn't get committed | |
482 | * after a system failure | |
483 | */ | |
484 | static int | |
867959b5 | 485 | zvol_replay_write(void *arg1, void *arg2, boolean_t byteswap) |
60101509 | 486 | { |
867959b5 BB |
487 | zvol_state_t *zv = arg1; |
488 | lr_write_t *lr = arg2; | |
60101509 | 489 | objset_t *os = zv->zv_objset; |
5c214ae3 BB |
490 | char *data = (char *)(lr + 1); /* data follows lr_write_t */ |
491 | uint64_t offset, length; | |
60101509 BB |
492 | dmu_tx_t *tx; |
493 | int error; | |
494 | ||
495 | if (byteswap) | |
496 | byteswap_uint64_array(lr, sizeof (*lr)); | |
497 | ||
5c214ae3 BB |
498 | offset = lr->lr_offset; |
499 | length = lr->lr_length; | |
500 | ||
501 | /* If it's a dmu_sync() block, write the whole block */ | |
502 | if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) { | |
503 | uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr); | |
504 | if (length < blocksize) { | |
505 | offset -= offset % blocksize; | |
506 | length = blocksize; | |
507 | } | |
508 | } | |
509 | ||
60101509 | 510 | tx = dmu_tx_create(os); |
5c214ae3 | 511 | dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length); |
60101509 BB |
512 | error = dmu_tx_assign(tx, TXG_WAIT); |
513 | if (error) { | |
514 | dmu_tx_abort(tx); | |
515 | } else { | |
5c214ae3 | 516 | dmu_write(os, ZVOL_OBJ, offset, length, data, tx); |
60101509 BB |
517 | dmu_tx_commit(tx); |
518 | } | |
519 | ||
5c214ae3 | 520 | return (error); |
60101509 BB |
521 | } |
522 | ||
523 | static int | |
867959b5 | 524 | zvol_replay_err(void *arg1, void *arg2, boolean_t byteswap) |
60101509 | 525 | { |
2e528b49 | 526 | return (SET_ERROR(ENOTSUP)); |
60101509 BB |
527 | } |
528 | ||
529 | /* | |
530 | * Callback vectors for replaying records. | |
460a0213 | 531 | * Only TX_WRITE and TX_TRUNCATE are needed for zvol. |
60101509 | 532 | */ |
867959b5 BB |
533 | zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = { |
534 | zvol_replay_err, /* no such transaction type */ | |
535 | zvol_replay_err, /* TX_CREATE */ | |
536 | zvol_replay_err, /* TX_MKDIR */ | |
537 | zvol_replay_err, /* TX_MKXATTR */ | |
538 | zvol_replay_err, /* TX_SYMLINK */ | |
539 | zvol_replay_err, /* TX_REMOVE */ | |
540 | zvol_replay_err, /* TX_RMDIR */ | |
541 | zvol_replay_err, /* TX_LINK */ | |
542 | zvol_replay_err, /* TX_RENAME */ | |
543 | zvol_replay_write, /* TX_WRITE */ | |
544 | zvol_replay_truncate, /* TX_TRUNCATE */ | |
545 | zvol_replay_err, /* TX_SETATTR */ | |
546 | zvol_replay_err, /* TX_ACL */ | |
547 | zvol_replay_err, /* TX_CREATE_ATTR */ | |
548 | zvol_replay_err, /* TX_CREATE_ACL_ATTR */ | |
549 | zvol_replay_err, /* TX_MKDIR_ACL */ | |
550 | zvol_replay_err, /* TX_MKDIR_ATTR */ | |
551 | zvol_replay_err, /* TX_MKDIR_ACL_ATTR */ | |
552 | zvol_replay_err, /* TX_WRITE2 */ | |
60101509 BB |
553 | }; |
554 | ||
555 | /* | |
556 | * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions. | |
557 | * | |
558 | * We store data in the log buffers if it's small enough. | |
559 | * Otherwise we will later flush the data out via dmu_sync(). | |
560 | */ | |
561 | ssize_t zvol_immediate_write_sz = 32768; | |
562 | ||
5df7e9d8 | 563 | void |
ce37ebd2 BB |
564 | zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, uint64_t offset, |
565 | uint64_t size, int sync) | |
60101509 BB |
566 | { |
567 | uint32_t blocksize = zv->zv_volblocksize; | |
568 | zilog_t *zilog = zv->zv_zilog; | |
1b7c1e5c | 569 | itx_wr_state_t write_state; |
60101509 BB |
570 | |
571 | if (zil_replaying(zilog, tx)) | |
572 | return; | |
573 | ||
1b7c1e5c GDN |
574 | if (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT) |
575 | write_state = WR_INDIRECT; | |
576 | else if (!spa_has_slogs(zilog->zl_spa) && | |
577 | size >= blocksize && blocksize > zvol_immediate_write_sz) | |
578 | write_state = WR_INDIRECT; | |
579 | else if (sync) | |
580 | write_state = WR_COPIED; | |
581 | else | |
582 | write_state = WR_NEED_COPY; | |
60101509 BB |
583 | |
584 | while (size) { | |
585 | itx_t *itx; | |
586 | lr_write_t *lr; | |
1b7c1e5c GDN |
587 | itx_wr_state_t wr_state = write_state; |
588 | ssize_t len = size; | |
60101509 | 589 | |
b8738257 | 590 | if (wr_state == WR_COPIED && size > zil_max_copied_data(zilog)) |
1b7c1e5c GDN |
591 | wr_state = WR_NEED_COPY; |
592 | else if (wr_state == WR_INDIRECT) | |
593 | len = MIN(blocksize - P2PHASE(offset, blocksize), size); | |
60101509 BB |
594 | |
595 | itx = zil_itx_create(TX_WRITE, sizeof (*lr) + | |
1b7c1e5c | 596 | (wr_state == WR_COPIED ? len : 0)); |
60101509 | 597 | lr = (lr_write_t *)&itx->itx_lr; |
5228cf01 RY |
598 | if (wr_state == WR_COPIED && dmu_read_by_dnode(zv->zv_dn, |
599 | offset, len, lr+1, DMU_READ_NO_PREFETCH) != 0) { | |
60101509 BB |
600 | zil_itx_destroy(itx); |
601 | itx = zil_itx_create(TX_WRITE, sizeof (*lr)); | |
602 | lr = (lr_write_t *)&itx->itx_lr; | |
1b7c1e5c | 603 | wr_state = WR_NEED_COPY; |
60101509 BB |
604 | } |
605 | ||
1b7c1e5c | 606 | itx->itx_wr_state = wr_state; |
60101509 BB |
607 | lr->lr_foid = ZVOL_OBJ; |
608 | lr->lr_offset = offset; | |
609 | lr->lr_length = len; | |
610 | lr->lr_blkoff = 0; | |
611 | BP_ZERO(&lr->lr_blkptr); | |
612 | ||
613 | itx->itx_private = zv; | |
614 | itx->itx_sync = sync; | |
615 | ||
616 | (void) zil_itx_assign(zilog, itx, tx); | |
617 | ||
618 | offset += len; | |
619 | size -= len; | |
620 | } | |
621 | } | |
622 | ||
460a0213 DM |
623 | /* |
624 | * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE. | |
625 | */ | |
5df7e9d8 | 626 | void |
460a0213 DM |
627 | zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off, uint64_t len, |
628 | boolean_t sync) | |
629 | { | |
630 | itx_t *itx; | |
631 | lr_truncate_t *lr; | |
632 | zilog_t *zilog = zv->zv_zilog; | |
633 | ||
634 | if (zil_replaying(zilog, tx)) | |
635 | return; | |
636 | ||
637 | itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr)); | |
638 | lr = (lr_truncate_t *)&itx->itx_lr; | |
639 | lr->lr_foid = ZVOL_OBJ; | |
640 | lr->lr_offset = off; | |
641 | lr->lr_length = len; | |
642 | ||
643 | itx->itx_sync = sync; | |
644 | zil_itx_assign(zilog, itx, tx); | |
645 | } | |
646 | ||
60101509 | 647 | |
1eacf2b3 JG |
648 | /* ARGSUSED */ |
649 | static void | |
650 | zvol_get_done(zgd_t *zgd, int error) | |
651 | { | |
652 | if (zgd->zgd_db) | |
653 | dmu_buf_rele(zgd->zgd_db, zgd); | |
654 | ||
655 | rangelock_exit(zgd->zgd_lr); | |
656 | ||
657 | kmem_free(zgd, sizeof (zgd_t)); | |
658 | } | |
659 | ||
660 | /* | |
661 | * Get data to generate a TX_WRITE intent log record. | |
662 | */ | |
5df7e9d8 | 663 | int |
1eacf2b3 JG |
664 | zvol_get_data(void *arg, lr_write_t *lr, char *buf, struct lwb *lwb, zio_t *zio) |
665 | { | |
666 | zvol_state_t *zv = arg; | |
667 | uint64_t offset = lr->lr_offset; | |
668 | uint64_t size = lr->lr_length; | |
669 | dmu_buf_t *db; | |
670 | zgd_t *zgd; | |
671 | int error; | |
672 | ||
673 | ASSERT3P(lwb, !=, NULL); | |
674 | ASSERT3P(zio, !=, NULL); | |
675 | ASSERT3U(size, !=, 0); | |
676 | ||
677 | zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP); | |
678 | zgd->zgd_lwb = lwb; | |
679 | ||
680 | /* | |
681 | * Write records come in two flavors: immediate and indirect. | |
682 | * For small writes it's cheaper to store the data with the | |
683 | * log record (immediate); for large writes it's cheaper to | |
684 | * sync the data and get a pointer to it (indirect) so that | |
685 | * we don't have to write the data twice. | |
686 | */ | |
687 | if (buf != NULL) { /* immediate write */ | |
688 | zgd->zgd_lr = rangelock_enter(&zv->zv_rangelock, offset, size, | |
689 | RL_READER); | |
690 | error = dmu_read_by_dnode(zv->zv_dn, offset, size, buf, | |
691 | DMU_READ_NO_PREFETCH); | |
692 | } else { /* indirect write */ | |
693 | /* | |
694 | * Have to lock the whole block to ensure when it's written out | |
695 | * and its checksum is being calculated that no one can change | |
696 | * the data. Contrarily to zfs_get_data we need not re-check | |
697 | * blocksize after we get the lock because it cannot be changed. | |
698 | */ | |
699 | size = zv->zv_volblocksize; | |
700 | offset = P2ALIGN_TYPED(offset, size, uint64_t); | |
701 | zgd->zgd_lr = rangelock_enter(&zv->zv_rangelock, offset, size, | |
702 | RL_READER); | |
703 | error = dmu_buf_hold_by_dnode(zv->zv_dn, offset, zgd, &db, | |
704 | DMU_READ_NO_PREFETCH); | |
705 | if (error == 0) { | |
706 | blkptr_t *bp = &lr->lr_blkptr; | |
707 | ||
708 | zgd->zgd_db = db; | |
709 | zgd->zgd_bp = bp; | |
710 | ||
711 | ASSERT(db != NULL); | |
712 | ASSERT(db->db_offset == offset); | |
713 | ASSERT(db->db_size == size); | |
714 | ||
715 | error = dmu_sync(zio, lr->lr_common.lrc_txg, | |
716 | zvol_get_done, zgd); | |
717 | ||
718 | if (error == 0) | |
719 | return (0); | |
720 | } | |
721 | } | |
722 | ||
723 | zvol_get_done(zgd, error); | |
724 | ||
725 | return (SET_ERROR(error)); | |
726 | } | |
727 | ||
60101509 | 728 | /* |
d45e010d | 729 | * The zvol_state_t's are inserted into zvol_state_list and zvol_htable. |
60101509 | 730 | */ |
5df7e9d8 MM |
731 | |
732 | void | |
d45e010d | 733 | zvol_insert(zvol_state_t *zv) |
60101509 | 734 | { |
7b98f0d9 | 735 | ASSERT(RW_WRITE_HELD(&zvol_state_lock)); |
d45e010d CC |
736 | list_insert_head(&zvol_state_list, zv); |
737 | hlist_add_head(&zv->zv_hlink, ZVOL_HT_HEAD(zv->zv_hash)); | |
60101509 BB |
738 | } |
739 | ||
740 | /* | |
741 | * Simply remove the zvol from to list of zvols. | |
742 | */ | |
743 | static void | |
d45e010d | 744 | zvol_remove(zvol_state_t *zv) |
60101509 | 745 | { |
7b98f0d9 | 746 | ASSERT(RW_WRITE_HELD(&zvol_state_lock)); |
d45e010d CC |
747 | list_remove(&zvol_state_list, zv); |
748 | hlist_del(&zv->zv_hlink); | |
60101509 BB |
749 | } |
750 | ||
040dab99 CC |
751 | /* |
752 | * Setup zv after we just own the zv->objset | |
753 | */ | |
60101509 | 754 | static int |
040dab99 | 755 | zvol_setup_zv(zvol_state_t *zv) |
60101509 | 756 | { |
60101509 BB |
757 | uint64_t volsize; |
758 | int error; | |
759 | uint64_t ro; | |
040dab99 | 760 | objset_t *os = zv->zv_objset; |
1ee159f4 | 761 | |
7b98f0d9 BB |
762 | ASSERT(MUTEX_HELD(&zv->zv_state_lock)); |
763 | ASSERT(RW_LOCK_HELD(&zv->zv_suspend_lock)); | |
58404a73 | 764 | |
1eacf2b3 JG |
765 | zv->zv_zilog = NULL; |
766 | zv->zv_flags &= ~ZVOL_WRITTEN_TO; | |
767 | ||
1ee159f4 BP |
768 | error = dsl_prop_get_integer(zv->zv_name, "readonly", &ro, NULL); |
769 | if (error) | |
040dab99 | 770 | return (SET_ERROR(error)); |
60101509 BB |
771 | |
772 | error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize); | |
1ee159f4 | 773 | if (error) |
040dab99 | 774 | return (SET_ERROR(error)); |
60101509 | 775 | |
5228cf01 | 776 | error = dnode_hold(os, ZVOL_OBJ, FTAG, &zv->zv_dn); |
1ee159f4 | 777 | if (error) |
040dab99 | 778 | return (SET_ERROR(error)); |
60101509 | 779 | |
5df7e9d8 | 780 | ops->zv_set_capacity(zv, volsize >> 9); |
60101509 | 781 | zv->zv_volsize = volsize; |
60101509 | 782 | |
a4430fce GW |
783 | if (ro || dmu_objset_is_snapshot(os) || |
784 | !spa_writeable(dmu_objset_spa(os))) { | |
5df7e9d8 | 785 | ops->zv_set_disk_ro(zv, 1); |
babf3f9b | 786 | zv->zv_flags |= ZVOL_RDONLY; |
60101509 | 787 | } else { |
5df7e9d8 | 788 | ops->zv_set_disk_ro(zv, 0); |
babf3f9b | 789 | zv->zv_flags &= ~ZVOL_RDONLY; |
60101509 | 790 | } |
040dab99 | 791 | return (0); |
60101509 BB |
792 | } |
793 | ||
040dab99 CC |
794 | /* |
795 | * Shutdown every zv_objset related stuff except zv_objset itself. | |
796 | * The is the reverse of zvol_setup_zv. | |
797 | */ | |
60101509 | 798 | static void |
040dab99 | 799 | zvol_shutdown_zv(zvol_state_t *zv) |
60101509 | 800 | { |
58404a73 BP |
801 | ASSERT(MUTEX_HELD(&zv->zv_state_lock) && |
802 | RW_LOCK_HELD(&zv->zv_suspend_lock)); | |
803 | ||
1eacf2b3 JG |
804 | if (zv->zv_flags & ZVOL_WRITTEN_TO) { |
805 | ASSERT(zv->zv_zilog != NULL); | |
806 | zil_close(zv->zv_zilog); | |
807 | } | |
808 | ||
60101509 | 809 | zv->zv_zilog = NULL; |
04434775 | 810 | |
5228cf01 RY |
811 | dnode_rele(zv->zv_dn, FTAG); |
812 | zv->zv_dn = NULL; | |
04434775 MA |
813 | |
814 | /* | |
47ab01a1 TC |
815 | * Evict cached data. We must write out any dirty data before |
816 | * disowning the dataset. | |
04434775 | 817 | */ |
1eacf2b3 | 818 | if (zv->zv_flags & ZVOL_WRITTEN_TO) |
04434775 MA |
819 | txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); |
820 | (void) dmu_objset_evict_dbufs(zv->zv_objset); | |
040dab99 CC |
821 | } |
822 | ||
823 | /* | |
824 | * return the proper tag for rollback and recv | |
825 | */ | |
826 | void * | |
827 | zvol_tag(zvol_state_t *zv) | |
828 | { | |
829 | ASSERT(RW_WRITE_HELD(&zv->zv_suspend_lock)); | |
830 | return (zv->zv_open_count > 0 ? zv : NULL); | |
831 | } | |
832 | ||
833 | /* | |
834 | * Suspend the zvol for recv and rollback. | |
835 | */ | |
836 | zvol_state_t * | |
837 | zvol_suspend(const char *name) | |
838 | { | |
839 | zvol_state_t *zv; | |
840 | ||
58404a73 BP |
841 | zv = zvol_find_by_name(name, RW_WRITER); |
842 | ||
843 | if (zv == NULL) | |
5559ba09 | 844 | return (NULL); |
04434775 | 845 | |
040dab99 | 846 | /* block all I/O, release in zvol_resume. */ |
7b98f0d9 BB |
847 | ASSERT(MUTEX_HELD(&zv->zv_state_lock)); |
848 | ASSERT(RW_WRITE_HELD(&zv->zv_suspend_lock)); | |
040dab99 CC |
849 | |
850 | atomic_inc(&zv->zv_suspend_ref); | |
851 | ||
852 | if (zv->zv_open_count > 0) | |
853 | zvol_shutdown_zv(zv); | |
5559ba09 | 854 | |
58404a73 BP |
855 | /* |
856 | * do not hold zv_state_lock across suspend/resume to | |
857 | * avoid locking up zvol lookups | |
858 | */ | |
5559ba09 | 859 | mutex_exit(&zv->zv_state_lock); |
58404a73 BP |
860 | |
861 | /* zv_suspend_lock is released in zvol_resume() */ | |
040dab99 CC |
862 | return (zv); |
863 | } | |
864 | ||
865 | int | |
866 | zvol_resume(zvol_state_t *zv) | |
867 | { | |
868 | int error = 0; | |
869 | ||
870 | ASSERT(RW_WRITE_HELD(&zv->zv_suspend_lock)); | |
2d82116e | 871 | |
58404a73 BP |
872 | mutex_enter(&zv->zv_state_lock); |
873 | ||
040dab99 CC |
874 | if (zv->zv_open_count > 0) { |
875 | VERIFY0(dmu_objset_hold(zv->zv_name, zv, &zv->zv_objset)); | |
876 | VERIFY3P(zv->zv_objset->os_dsl_dataset->ds_owner, ==, zv); | |
877 | VERIFY(dsl_dataset_long_held(zv->zv_objset->os_dsl_dataset)); | |
878 | dmu_objset_rele(zv->zv_objset, zv); | |
879 | ||
880 | error = zvol_setup_zv(zv); | |
881 | } | |
58404a73 BP |
882 | |
883 | mutex_exit(&zv->zv_state_lock); | |
884 | ||
040dab99 CC |
885 | rw_exit(&zv->zv_suspend_lock); |
886 | /* | |
887 | * We need this because we don't hold zvol_state_lock while releasing | |
888 | * zv_suspend_lock. zvol_remove_minors_impl thus cannot check | |
889 | * zv_suspend_lock to determine it is safe to free because rwlock is | |
890 | * not inherent atomic. | |
891 | */ | |
892 | atomic_dec(&zv->zv_suspend_ref); | |
893 | ||
894 | return (SET_ERROR(error)); | |
895 | } | |
896 | ||
5df7e9d8 | 897 | int |
163a8c28 | 898 | zvol_first_open(zvol_state_t *zv, boolean_t readonly) |
040dab99 CC |
899 | { |
900 | objset_t *os; | |
07783588 | 901 | int error, locked = 0; |
163a8c28 | 902 | boolean_t ro; |
07783588 | 903 | |
58404a73 BP |
904 | ASSERT(RW_READ_HELD(&zv->zv_suspend_lock)); |
905 | ASSERT(MUTEX_HELD(&zv->zv_state_lock)); | |
906 | ||
07783588 BP |
907 | /* |
908 | * In all other cases the spa_namespace_lock is taken before the | |
909 | * bdev->bd_mutex lock. But in this case the Linux __blkdev_get() | |
910 | * function calls fops->open() with the bdev->bd_mutex lock held. | |
911 | * This deadlock can be easily observed with zvols used as vdevs. | |
912 | * | |
913 | * To avoid a potential lock inversion deadlock we preemptively | |
914 | * try to take the spa_namespace_lock(). Normally it will not | |
915 | * be contended and this is safe because spa_open_common() handles | |
916 | * the case where the caller already holds the spa_namespace_lock. | |
917 | * | |
918 | * When it is contended we risk a lock inversion if we were to | |
919 | * block waiting for the lock. Luckily, the __blkdev_get() | |
920 | * function allows us to return -ERESTARTSYS which will result in | |
921 | * bdev->bd_mutex being dropped, reacquired, and fops->open() being | |
922 | * called again. This process can be repeated safely until both | |
923 | * locks are acquired. | |
924 | */ | |
925 | if (!mutex_owned(&spa_namespace_lock)) { | |
926 | locked = mutex_tryenter(&spa_namespace_lock); | |
927 | if (!locked) | |
5df7e9d8 | 928 | return (SET_ERROR(EINTR)); |
07783588 | 929 | } |
040dab99 | 930 | |
163a8c28 TC |
931 | ro = (readonly || (strchr(zv->zv_name, '@') != NULL)); |
932 | error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, ro, B_TRUE, zv, &os); | |
040dab99 | 933 | if (error) |
07783588 | 934 | goto out_mutex; |
040dab99 CC |
935 | |
936 | zv->zv_objset = os; | |
937 | ||
938 | error = zvol_setup_zv(zv); | |
939 | ||
940 | if (error) { | |
b5256303 | 941 | dmu_objset_disown(os, 1, zv); |
040dab99 CC |
942 | zv->zv_objset = NULL; |
943 | } | |
944 | ||
07783588 BP |
945 | out_mutex: |
946 | if (locked) | |
947 | mutex_exit(&spa_namespace_lock); | |
5df7e9d8 | 948 | return (SET_ERROR(error)); |
040dab99 CC |
949 | } |
950 | ||
5df7e9d8 | 951 | void |
040dab99 CC |
952 | zvol_last_close(zvol_state_t *zv) |
953 | { | |
58404a73 BP |
954 | ASSERT(RW_READ_HELD(&zv->zv_suspend_lock)); |
955 | ASSERT(MUTEX_HELD(&zv->zv_state_lock)); | |
956 | ||
040dab99 CC |
957 | zvol_shutdown_zv(zv); |
958 | ||
b5256303 | 959 | dmu_objset_disown(zv->zv_objset, 1, zv); |
60101509 BB |
960 | zv->zv_objset = NULL; |
961 | } | |
962 | ||
7ac557ce CC |
963 | typedef struct minors_job { |
964 | list_t *list; | |
965 | list_node_t link; | |
966 | /* input */ | |
967 | char *name; | |
968 | /* output */ | |
969 | int error; | |
970 | } minors_job_t; | |
971 | ||
972 | /* | |
973 | * Prefetch zvol dnodes for the minors_job | |
974 | */ | |
975 | static void | |
976 | zvol_prefetch_minors_impl(void *arg) | |
977 | { | |
978 | minors_job_t *job = arg; | |
979 | char *dsname = job->name; | |
980 | objset_t *os = NULL; | |
981 | ||
b5256303 TC |
982 | job->error = dmu_objset_own(dsname, DMU_OST_ZVOL, B_TRUE, B_TRUE, |
983 | FTAG, &os); | |
7ac557ce CC |
984 | if (job->error == 0) { |
985 | dmu_prefetch(os, ZVOL_OBJ, 0, 0, 0, ZIO_PRIORITY_SYNC_READ); | |
b5256303 | 986 | dmu_objset_disown(os, B_TRUE, FTAG); |
7ac557ce CC |
987 | } |
988 | } | |
a0bd735a BP |
989 | |
990 | /* | |
991 | * Mask errors to continue dmu_objset_find() traversal | |
992 | */ | |
993 | static int | |
994 | zvol_create_snap_minor_cb(const char *dsname, void *arg) | |
995 | { | |
7ac557ce CC |
996 | minors_job_t *j = arg; |
997 | list_t *minors_list = j->list; | |
998 | const char *name = j->name; | |
a0bd735a | 999 | |
1ee159f4 BP |
1000 | ASSERT0(MUTEX_HELD(&spa_namespace_lock)); |
1001 | ||
a0bd735a BP |
1002 | /* skip the designated dataset */ |
1003 | if (name && strcmp(dsname, name) == 0) | |
1004 | return (0); | |
1005 | ||
1006 | /* at this point, the dsname should name a snapshot */ | |
1007 | if (strchr(dsname, '@') == 0) { | |
1008 | dprintf("zvol_create_snap_minor_cb(): " | |
e1cfd73f | 1009 | "%s is not a snapshot name\n", dsname); |
a0bd735a | 1010 | } else { |
7ac557ce CC |
1011 | minors_job_t *job; |
1012 | char *n = strdup(dsname); | |
1013 | if (n == NULL) | |
1014 | return (0); | |
1015 | ||
1016 | job = kmem_alloc(sizeof (minors_job_t), KM_SLEEP); | |
1017 | job->name = n; | |
1018 | job->list = minors_list; | |
1019 | job->error = 0; | |
1020 | list_insert_tail(minors_list, job); | |
1021 | /* don't care if dispatch fails, because job->error is 0 */ | |
1022 | taskq_dispatch(system_taskq, zvol_prefetch_minors_impl, job, | |
1023 | TQ_SLEEP); | |
a0bd735a BP |
1024 | } |
1025 | ||
1026 | return (0); | |
1027 | } | |
1028 | ||
1029 | /* | |
1030 | * Mask errors to continue dmu_objset_find() traversal | |
1031 | */ | |
60101509 | 1032 | static int |
13fe0198 | 1033 | zvol_create_minors_cb(const char *dsname, void *arg) |
60101509 | 1034 | { |
a0bd735a BP |
1035 | uint64_t snapdev; |
1036 | int error; | |
7ac557ce | 1037 | list_t *minors_list = arg; |
a0bd735a | 1038 | |
1ee159f4 BP |
1039 | ASSERT0(MUTEX_HELD(&spa_namespace_lock)); |
1040 | ||
a0bd735a BP |
1041 | error = dsl_prop_get_integer(dsname, "snapdev", &snapdev, NULL); |
1042 | if (error) | |
1043 | return (0); | |
1044 | ||
1045 | /* | |
1046 | * Given the name and the 'snapdev' property, create device minor nodes | |
1047 | * with the linkages to zvols/snapshots as needed. | |
1048 | * If the name represents a zvol, create a minor node for the zvol, then | |
1049 | * check if its snapshots are 'visible', and if so, iterate over the | |
1050 | * snapshots and create device minor nodes for those. | |
1051 | */ | |
1052 | if (strchr(dsname, '@') == 0) { | |
7ac557ce CC |
1053 | minors_job_t *job; |
1054 | char *n = strdup(dsname); | |
1055 | if (n == NULL) | |
1056 | return (0); | |
1057 | ||
1058 | job = kmem_alloc(sizeof (minors_job_t), KM_SLEEP); | |
1059 | job->name = n; | |
1060 | job->list = minors_list; | |
1061 | job->error = 0; | |
1062 | list_insert_tail(minors_list, job); | |
1063 | /* don't care if dispatch fails, because job->error is 0 */ | |
1064 | taskq_dispatch(system_taskq, zvol_prefetch_minors_impl, job, | |
1065 | TQ_SLEEP); | |
1066 | ||
1067 | if (snapdev == ZFS_SNAPDEV_VISIBLE) { | |
a0bd735a BP |
1068 | /* |
1069 | * traverse snapshots only, do not traverse children, | |
1070 | * and skip the 'dsname' | |
1071 | */ | |
5df7e9d8 | 1072 | error = dmu_objset_find(dsname, |
7ac557ce | 1073 | zvol_create_snap_minor_cb, (void *)job, |
a0bd735a | 1074 | DS_FIND_SNAPSHOTS); |
a0bd735a BP |
1075 | } |
1076 | } else { | |
1077 | dprintf("zvol_create_minors_cb(): %s is not a zvol name\n", | |
02730c33 | 1078 | dsname); |
a0bd735a | 1079 | } |
60101509 | 1080 | |
d5674448 | 1081 | return (0); |
60101509 BB |
1082 | } |
1083 | ||
1084 | /* | |
a0bd735a BP |
1085 | * Create minors for the specified dataset, including children and snapshots. |
1086 | * Pay attention to the 'snapdev' property and iterate over the snapshots | |
1087 | * only if they are 'visible'. This approach allows one to assure that the | |
1088 | * snapshot metadata is read from disk only if it is needed. | |
1089 | * | |
1090 | * The name can represent a dataset to be recursively scanned for zvols and | |
1091 | * their snapshots, or a single zvol snapshot. If the name represents a | |
1092 | * dataset, the scan is performed in two nested stages: | |
1093 | * - scan the dataset for zvols, and | |
1094 | * - for each zvol, create a minor node, then check if the zvol's snapshots | |
1095 | * are 'visible', and only then iterate over the snapshots if needed | |
1096 | * | |
4e33ba4c | 1097 | * If the name represents a snapshot, a check is performed if the snapshot is |
a0bd735a BP |
1098 | * 'visible' (which also verifies that the parent is a zvol), and if so, |
1099 | * a minor node for that snapshot is created. | |
60101509 | 1100 | */ |
a0bd735a BP |
1101 | static int |
1102 | zvol_create_minors_impl(const char *name) | |
60101509 | 1103 | { |
60101509 | 1104 | int error = 0; |
5428dc51 | 1105 | fstrans_cookie_t cookie; |
a0bd735a | 1106 | char *atp, *parent; |
7ac557ce CC |
1107 | list_t minors_list; |
1108 | minors_job_t *job; | |
60101509 | 1109 | |
5428dc51 BP |
1110 | if (zvol_inhibit_dev) |
1111 | return (0); | |
1112 | ||
7ac557ce CC |
1113 | /* |
1114 | * This is the list for prefetch jobs. Whenever we found a match | |
1115 | * during dmu_objset_find, we insert a minors_job to the list and do | |
1116 | * taskq_dispatch to parallel prefetch zvol dnodes. Note we don't need | |
1117 | * any lock because all list operation is done on the current thread. | |
1118 | * | |
1119 | * We will use this list to do zvol_create_minor_impl after prefetch | |
1120 | * so we don't have to traverse using dmu_objset_find again. | |
1121 | */ | |
1122 | list_create(&minors_list, sizeof (minors_job_t), | |
1123 | offsetof(minors_job_t, link)); | |
1124 | ||
a0bd735a BP |
1125 | parent = kmem_alloc(MAXPATHLEN, KM_SLEEP); |
1126 | (void) strlcpy(parent, name, MAXPATHLEN); | |
1127 | ||
1128 | if ((atp = strrchr(parent, '@')) != NULL) { | |
1129 | uint64_t snapdev; | |
1130 | ||
1131 | *atp = '\0'; | |
1132 | error = dsl_prop_get_integer(parent, "snapdev", | |
1133 | &snapdev, NULL); | |
1134 | ||
1135 | if (error == 0 && snapdev == ZFS_SNAPDEV_VISIBLE) | |
5df7e9d8 | 1136 | error = ops->zv_create_minor(name); |
a0bd735a BP |
1137 | } else { |
1138 | cookie = spl_fstrans_mark(); | |
1139 | error = dmu_objset_find(parent, zvol_create_minors_cb, | |
7ac557ce | 1140 | &minors_list, DS_FIND_CHILDREN); |
a0bd735a BP |
1141 | spl_fstrans_unmark(cookie); |
1142 | } | |
1143 | ||
1144 | kmem_free(parent, MAXPATHLEN); | |
7ac557ce CC |
1145 | taskq_wait_outstanding(system_taskq, 0); |
1146 | ||
1147 | /* | |
1148 | * Prefetch is completed, we can do zvol_create_minor_impl | |
1149 | * sequentially. | |
1150 | */ | |
1151 | while ((job = list_head(&minors_list)) != NULL) { | |
1152 | list_remove(&minors_list, job); | |
1153 | if (!job->error) | |
5df7e9d8 | 1154 | ops->zv_create_minor(job->name); |
7ac557ce CC |
1155 | strfree(job->name); |
1156 | kmem_free(job, sizeof (minors_job_t)); | |
1157 | } | |
1158 | ||
1159 | list_destroy(&minors_list); | |
ba6a2402 BB |
1160 | |
1161 | return (SET_ERROR(error)); | |
1162 | } | |
1163 | ||
1164 | /* | |
1165 | * Remove minors for specified dataset including children and snapshots. | |
1166 | */ | |
5df7e9d8 MM |
1167 | |
1168 | void | |
a0bd735a | 1169 | zvol_remove_minors_impl(const char *name) |
ba6a2402 BB |
1170 | { |
1171 | zvol_state_t *zv, *zv_next; | |
1172 | int namelen = ((name) ? strlen(name) : 0); | |
899662e3 | 1173 | taskqid_t t, tid = TASKQID_INVALID; |
5559ba09 | 1174 | list_t free_list; |
ba6a2402 | 1175 | |
74497b7a | 1176 | if (zvol_inhibit_dev) |
ba6a2402 | 1177 | return; |
74497b7a | 1178 | |
5559ba09 BP |
1179 | list_create(&free_list, sizeof (zvol_state_t), |
1180 | offsetof(zvol_state_t, zv_next)); | |
1181 | ||
7b98f0d9 | 1182 | rw_enter(&zvol_state_lock, RW_WRITER); |
ba6a2402 BB |
1183 | |
1184 | for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) { | |
1185 | zv_next = list_next(&zvol_state_list, zv); | |
1186 | ||
58404a73 | 1187 | mutex_enter(&zv->zv_state_lock); |
ba6a2402 BB |
1188 | if (name == NULL || strcmp(zv->zv_name, name) == 0 || |
1189 | (strncmp(zv->zv_name, name, namelen) == 0 && | |
5428dc51 BP |
1190 | (zv->zv_name[namelen] == '/' || |
1191 | zv->zv_name[namelen] == '@'))) { | |
5559ba09 | 1192 | /* |
58404a73 | 1193 | * By holding zv_state_lock here, we guarantee that no |
5559ba09 BP |
1194 | * one is currently using this zv |
1195 | */ | |
97f8d796 | 1196 | |
1197 | /* If in use, leave alone */ | |
1198 | if (zv->zv_open_count > 0 || | |
1199 | atomic_read(&zv->zv_suspend_ref)) { | |
1200 | mutex_exit(&zv->zv_state_lock); | |
1201 | continue; | |
1202 | } | |
1203 | ||
ba6a2402 | 1204 | zvol_remove(zv); |
899662e3 | 1205 | |
58404a73 | 1206 | /* |
7b98f0d9 BB |
1207 | * Cleared while holding zvol_state_lock as a writer |
1208 | * which will prevent zvol_open() from opening it. | |
58404a73 | 1209 | */ |
5df7e9d8 | 1210 | ops->zv_clear_private(zv); |
899662e3 | 1211 | |
97f8d796 | 1212 | /* Drop zv_state_lock before zvol_free() */ |
1213 | mutex_exit(&zv->zv_state_lock); | |
1214 | ||
7b98f0d9 | 1215 | /* Try parallel zv_free, if failed do it in place */ |
5df7e9d8 MM |
1216 | t = taskq_dispatch(system_taskq, |
1217 | (task_func_t *)ops->zv_free, zv, TQ_SLEEP); | |
899662e3 | 1218 | if (t == TASKQID_INVALID) |
5559ba09 | 1219 | list_insert_head(&free_list, zv); |
899662e3 CC |
1220 | else |
1221 | tid = t; | |
58404a73 BP |
1222 | } else { |
1223 | mutex_exit(&zv->zv_state_lock); | |
60101509 | 1224 | } |
60101509 | 1225 | } |
7b98f0d9 | 1226 | rw_exit(&zvol_state_lock); |
5559ba09 | 1227 | |
7b98f0d9 | 1228 | /* Drop zvol_state_lock before calling zvol_free() */ |
5559ba09 BP |
1229 | while ((zv = list_head(&free_list)) != NULL) { |
1230 | list_remove(&free_list, zv); | |
5df7e9d8 | 1231 | ops->zv_free(zv); |
5559ba09 BP |
1232 | } |
1233 | ||
899662e3 CC |
1234 | if (tid != TASKQID_INVALID) |
1235 | taskq_wait_outstanding(system_taskq, tid); | |
60101509 BB |
1236 | } |
1237 | ||
cf8738d8 | 1238 | /* Remove minor for this specific volume only */ |
a0bd735a BP |
1239 | static void |
1240 | zvol_remove_minor_impl(const char *name) | |
1241 | { | |
92aceb2a | 1242 | zvol_state_t *zv = NULL, *zv_next; |
a0bd735a BP |
1243 | |
1244 | if (zvol_inhibit_dev) | |
1245 | return; | |
1246 | ||
7b98f0d9 | 1247 | rw_enter(&zvol_state_lock, RW_WRITER); |
a0bd735a BP |
1248 | |
1249 | for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) { | |
1250 | zv_next = list_next(&zvol_state_list, zv); | |
1251 | ||
58404a73 | 1252 | mutex_enter(&zv->zv_state_lock); |
a0bd735a | 1253 | if (strcmp(zv->zv_name, name) == 0) { |
5559ba09 | 1254 | /* |
58404a73 | 1255 | * By holding zv_state_lock here, we guarantee that no |
5559ba09 BP |
1256 | * one is currently using this zv |
1257 | */ | |
97f8d796 | 1258 | |
1259 | /* If in use, leave alone */ | |
1260 | if (zv->zv_open_count > 0 || | |
1261 | atomic_read(&zv->zv_suspend_ref)) { | |
1262 | mutex_exit(&zv->zv_state_lock); | |
1263 | continue; | |
1264 | } | |
a0bd735a | 1265 | zvol_remove(zv); |
97f8d796 | 1266 | |
5df7e9d8 | 1267 | ops->zv_clear_private(zv); |
97f8d796 | 1268 | mutex_exit(&zv->zv_state_lock); |
a0bd735a | 1269 | break; |
58404a73 BP |
1270 | } else { |
1271 | mutex_exit(&zv->zv_state_lock); | |
a0bd735a BP |
1272 | } |
1273 | } | |
1274 | ||
92aceb2a | 1275 | /* Drop zvol_state_lock before calling zvol_free() */ |
7b98f0d9 | 1276 | rw_exit(&zvol_state_lock); |
5559ba09 | 1277 | |
92aceb2a | 1278 | if (zv != NULL) |
5df7e9d8 | 1279 | ops->zv_free(zv); |
a0bd735a BP |
1280 | } |
1281 | ||
60101509 | 1282 | /* |
ba6a2402 | 1283 | * Rename minors for specified dataset including children and snapshots. |
60101509 | 1284 | */ |
a0bd735a BP |
1285 | static void |
1286 | zvol_rename_minors_impl(const char *oldname, const char *newname) | |
60101509 BB |
1287 | { |
1288 | zvol_state_t *zv, *zv_next; | |
ba6a2402 | 1289 | int oldnamelen, newnamelen; |
60101509 | 1290 | |
74497b7a DH |
1291 | if (zvol_inhibit_dev) |
1292 | return; | |
1293 | ||
ba6a2402 BB |
1294 | oldnamelen = strlen(oldname); |
1295 | newnamelen = strlen(newname); | |
60101509 | 1296 | |
7b98f0d9 | 1297 | rw_enter(&zvol_state_lock, RW_READER); |
ba6a2402 | 1298 | |
60101509 BB |
1299 | for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) { |
1300 | zv_next = list_next(&zvol_state_list, zv); | |
1301 | ||
58404a73 BP |
1302 | mutex_enter(&zv->zv_state_lock); |
1303 | ||
ba6a2402 | 1304 | if (strcmp(zv->zv_name, oldname) == 0) { |
5df7e9d8 | 1305 | ops->zv_rename_minor(zv, newname); |
ba6a2402 BB |
1306 | } else if (strncmp(zv->zv_name, oldname, oldnamelen) == 0 && |
1307 | (zv->zv_name[oldnamelen] == '/' || | |
1308 | zv->zv_name[oldnamelen] == '@')) { | |
682ce104 | 1309 | char *name = kmem_asprintf("%s%c%s", newname, |
ba6a2402 BB |
1310 | zv->zv_name[oldnamelen], |
1311 | zv->zv_name + oldnamelen + 1); | |
5df7e9d8 | 1312 | ops->zv_rename_minor(zv, name); |
b5d69358 | 1313 | strfree(name); |
60101509 | 1314 | } |
58404a73 BP |
1315 | |
1316 | mutex_exit(&zv->zv_state_lock); | |
60101509 | 1317 | } |
ba6a2402 | 1318 | |
7b98f0d9 | 1319 | rw_exit(&zvol_state_lock); |
60101509 BB |
1320 | } |
1321 | ||
a0bd735a BP |
1322 | typedef struct zvol_snapdev_cb_arg { |
1323 | uint64_t snapdev; | |
1324 | } zvol_snapdev_cb_arg_t; | |
1325 | ||
0b4d1b58 | 1326 | static int |
4ea3f864 GM |
1327 | zvol_set_snapdev_cb(const char *dsname, void *param) |
1328 | { | |
a0bd735a | 1329 | zvol_snapdev_cb_arg_t *arg = param; |
0b4d1b58 ED |
1330 | |
1331 | if (strchr(dsname, '@') == NULL) | |
ba6a2402 | 1332 | return (0); |
0b4d1b58 | 1333 | |
a0bd735a | 1334 | switch (arg->snapdev) { |
0b4d1b58 | 1335 | case ZFS_SNAPDEV_VISIBLE: |
5df7e9d8 | 1336 | (void) ops->zv_create_minor(dsname); |
0b4d1b58 ED |
1337 | break; |
1338 | case ZFS_SNAPDEV_HIDDEN: | |
a0bd735a | 1339 | (void) zvol_remove_minor_impl(dsname); |
0b4d1b58 ED |
1340 | break; |
1341 | } | |
ba6a2402 BB |
1342 | |
1343 | return (0); | |
0b4d1b58 ED |
1344 | } |
1345 | ||
a0bd735a BP |
1346 | static void |
1347 | zvol_set_snapdev_impl(char *name, uint64_t snapdev) | |
1348 | { | |
1349 | zvol_snapdev_cb_arg_t arg = {snapdev}; | |
1350 | fstrans_cookie_t cookie = spl_fstrans_mark(); | |
1351 | /* | |
1352 | * The zvol_set_snapdev_sync() sets snapdev appropriately | |
1353 | * in the dataset hierarchy. Here, we only scan snapshots. | |
1354 | */ | |
1355 | dmu_objset_find(name, zvol_set_snapdev_cb, &arg, DS_FIND_SNAPSHOTS); | |
1356 | spl_fstrans_unmark(cookie); | |
1357 | } | |
1358 | ||
cf8738d8 | 1359 | typedef struct zvol_volmode_cb_arg { |
1360 | uint64_t volmode; | |
1361 | } zvol_volmode_cb_arg_t; | |
1362 | ||
1363 | static void | |
1364 | zvol_set_volmode_impl(char *name, uint64_t volmode) | |
1365 | { | |
1366 | fstrans_cookie_t cookie = spl_fstrans_mark(); | |
1367 | ||
1368 | if (strchr(name, '@') != NULL) | |
1369 | return; | |
1370 | ||
1371 | /* | |
1372 | * It's unfortunate we need to remove minors before we create new ones: | |
1373 | * this is necessary because our backing gendisk (zvol_state->zv_disk) | |
1374 | * coule be different when we set, for instance, volmode from "geom" | |
1375 | * to "dev" (or vice versa). | |
1376 | * A possible optimization is to modify our consumers so we don't get | |
1377 | * called when "volmode" does not change. | |
1378 | */ | |
1379 | switch (volmode) { | |
1380 | case ZFS_VOLMODE_NONE: | |
1381 | (void) zvol_remove_minor_impl(name); | |
1382 | break; | |
1383 | case ZFS_VOLMODE_GEOM: | |
1384 | case ZFS_VOLMODE_DEV: | |
1385 | (void) zvol_remove_minor_impl(name); | |
5df7e9d8 | 1386 | (void) ops->zv_create_minor(name); |
cf8738d8 | 1387 | break; |
1388 | case ZFS_VOLMODE_DEFAULT: | |
1389 | (void) zvol_remove_minor_impl(name); | |
1390 | if (zvol_volmode == ZFS_VOLMODE_NONE) | |
1391 | break; | |
1392 | else /* if zvol_volmode is invalid defaults to "geom" */ | |
5df7e9d8 | 1393 | (void) ops->zv_create_minor(name); |
cf8738d8 | 1394 | break; |
1395 | } | |
1396 | ||
1397 | spl_fstrans_unmark(cookie); | |
1398 | } | |
1399 | ||
a0bd735a BP |
1400 | static zvol_task_t * |
1401 | zvol_task_alloc(zvol_async_op_t op, const char *name1, const char *name2, | |
cf8738d8 | 1402 | uint64_t value) |
a0bd735a BP |
1403 | { |
1404 | zvol_task_t *task; | |
1405 | char *delim; | |
1406 | ||
1407 | /* Never allow tasks on hidden names. */ | |
1408 | if (name1[0] == '$') | |
1409 | return (NULL); | |
1410 | ||
1411 | task = kmem_zalloc(sizeof (zvol_task_t), KM_SLEEP); | |
1412 | task->op = op; | |
cf8738d8 | 1413 | task->value = value; |
a0bd735a BP |
1414 | delim = strchr(name1, '/'); |
1415 | strlcpy(task->pool, name1, delim ? (delim - name1 + 1) : MAXNAMELEN); | |
1416 | ||
1417 | strlcpy(task->name1, name1, MAXNAMELEN); | |
1418 | if (name2 != NULL) | |
1419 | strlcpy(task->name2, name2, MAXNAMELEN); | |
1420 | ||
1421 | return (task); | |
1422 | } | |
1423 | ||
1424 | static void | |
1425 | zvol_task_free(zvol_task_t *task) | |
1426 | { | |
1427 | kmem_free(task, sizeof (zvol_task_t)); | |
1428 | } | |
1429 | ||
1430 | /* | |
1431 | * The worker thread function performed asynchronously. | |
1432 | */ | |
1433 | static void | |
1434 | zvol_task_cb(void *param) | |
1435 | { | |
1436 | zvol_task_t *task = (zvol_task_t *)param; | |
1437 | ||
1438 | switch (task->op) { | |
1439 | case ZVOL_ASYNC_CREATE_MINORS: | |
1440 | (void) zvol_create_minors_impl(task->name1); | |
1441 | break; | |
1442 | case ZVOL_ASYNC_REMOVE_MINORS: | |
1443 | zvol_remove_minors_impl(task->name1); | |
1444 | break; | |
1445 | case ZVOL_ASYNC_RENAME_MINORS: | |
1446 | zvol_rename_minors_impl(task->name1, task->name2); | |
1447 | break; | |
1448 | case ZVOL_ASYNC_SET_SNAPDEV: | |
cf8738d8 | 1449 | zvol_set_snapdev_impl(task->name1, task->value); |
1450 | break; | |
1451 | case ZVOL_ASYNC_SET_VOLMODE: | |
1452 | zvol_set_volmode_impl(task->name1, task->value); | |
a0bd735a BP |
1453 | break; |
1454 | default: | |
1455 | VERIFY(0); | |
1456 | break; | |
1457 | } | |
1458 | ||
1459 | zvol_task_free(task); | |
1460 | } | |
1461 | ||
cf8738d8 | 1462 | typedef struct zvol_set_prop_int_arg { |
a0bd735a BP |
1463 | const char *zsda_name; |
1464 | uint64_t zsda_value; | |
1465 | zprop_source_t zsda_source; | |
1466 | dmu_tx_t *zsda_tx; | |
cf8738d8 | 1467 | } zvol_set_prop_int_arg_t; |
a0bd735a BP |
1468 | |
1469 | /* | |
1470 | * Sanity check the dataset for safe use by the sync task. No additional | |
1471 | * conditions are imposed. | |
1472 | */ | |
1473 | static int | |
1474 | zvol_set_snapdev_check(void *arg, dmu_tx_t *tx) | |
1475 | { | |
cf8738d8 | 1476 | zvol_set_prop_int_arg_t *zsda = arg; |
a0bd735a BP |
1477 | dsl_pool_t *dp = dmu_tx_pool(tx); |
1478 | dsl_dir_t *dd; | |
1479 | int error; | |
1480 | ||
1481 | error = dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL); | |
1482 | if (error != 0) | |
1483 | return (error); | |
1484 | ||
1485 | dsl_dir_rele(dd, FTAG); | |
1486 | ||
1487 | return (error); | |
1488 | } | |
1489 | ||
92aceb2a | 1490 | /* ARGSUSED */ |
a0bd735a BP |
1491 | static int |
1492 | zvol_set_snapdev_sync_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) | |
1493 | { | |
a0bd735a BP |
1494 | char dsname[MAXNAMELEN]; |
1495 | zvol_task_t *task; | |
92aceb2a | 1496 | uint64_t snapdev; |
a0bd735a BP |
1497 | |
1498 | dsl_dataset_name(ds, dsname); | |
92aceb2a | 1499 | if (dsl_prop_get_int_ds(ds, "snapdev", &snapdev) != 0) |
1500 | return (0); | |
1501 | task = zvol_task_alloc(ZVOL_ASYNC_SET_SNAPDEV, dsname, NULL, snapdev); | |
a0bd735a BP |
1502 | if (task == NULL) |
1503 | return (0); | |
1504 | ||
1505 | (void) taskq_dispatch(dp->dp_spa->spa_zvol_taskq, zvol_task_cb, | |
02730c33 | 1506 | task, TQ_SLEEP); |
a0bd735a BP |
1507 | return (0); |
1508 | } | |
1509 | ||
1510 | /* | |
92aceb2a | 1511 | * Traverse all child datasets and apply snapdev appropriately. |
1512 | * We call dsl_prop_set_sync_impl() here to set the value only on the toplevel | |
1513 | * dataset and read the effective "snapdev" on every child in the callback | |
1514 | * function: this is because the value is not guaranteed to be the same in the | |
1515 | * whole dataset hierarchy. | |
a0bd735a BP |
1516 | */ |
1517 | static void | |
1518 | zvol_set_snapdev_sync(void *arg, dmu_tx_t *tx) | |
1519 | { | |
cf8738d8 | 1520 | zvol_set_prop_int_arg_t *zsda = arg; |
a0bd735a BP |
1521 | dsl_pool_t *dp = dmu_tx_pool(tx); |
1522 | dsl_dir_t *dd; | |
92aceb2a | 1523 | dsl_dataset_t *ds; |
1524 | int error; | |
a0bd735a BP |
1525 | |
1526 | VERIFY0(dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL)); | |
1527 | zsda->zsda_tx = tx; | |
1528 | ||
92aceb2a | 1529 | error = dsl_dataset_hold(dp, zsda->zsda_name, FTAG, &ds); |
1530 | if (error == 0) { | |
1531 | dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_SNAPDEV), | |
1532 | zsda->zsda_source, sizeof (zsda->zsda_value), 1, | |
1533 | &zsda->zsda_value, zsda->zsda_tx); | |
1534 | dsl_dataset_rele(ds, FTAG); | |
1535 | } | |
a0bd735a BP |
1536 | dmu_objset_find_dp(dp, dd->dd_object, zvol_set_snapdev_sync_cb, |
1537 | zsda, DS_FIND_CHILDREN); | |
1538 | ||
1539 | dsl_dir_rele(dd, FTAG); | |
1540 | } | |
1541 | ||
0b4d1b58 | 1542 | int |
a0bd735a BP |
1543 | zvol_set_snapdev(const char *ddname, zprop_source_t source, uint64_t snapdev) |
1544 | { | |
cf8738d8 | 1545 | zvol_set_prop_int_arg_t zsda; |
5428dc51 | 1546 | |
a0bd735a BP |
1547 | zsda.zsda_name = ddname; |
1548 | zsda.zsda_source = source; | |
1549 | zsda.zsda_value = snapdev; | |
5428dc51 | 1550 | |
a0bd735a BP |
1551 | return (dsl_sync_task(ddname, zvol_set_snapdev_check, |
1552 | zvol_set_snapdev_sync, &zsda, 0, ZFS_SPACE_CHECK_NONE)); | |
1553 | } | |
1554 | ||
cf8738d8 | 1555 | /* |
1556 | * Sanity check the dataset for safe use by the sync task. No additional | |
1557 | * conditions are imposed. | |
1558 | */ | |
1559 | static int | |
1560 | zvol_set_volmode_check(void *arg, dmu_tx_t *tx) | |
1561 | { | |
1562 | zvol_set_prop_int_arg_t *zsda = arg; | |
1563 | dsl_pool_t *dp = dmu_tx_pool(tx); | |
1564 | dsl_dir_t *dd; | |
1565 | int error; | |
1566 | ||
1567 | error = dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL); | |
1568 | if (error != 0) | |
1569 | return (error); | |
1570 | ||
1571 | dsl_dir_rele(dd, FTAG); | |
1572 | ||
1573 | return (error); | |
1574 | } | |
1575 | ||
1576 | /* ARGSUSED */ | |
1577 | static int | |
1578 | zvol_set_volmode_sync_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) | |
1579 | { | |
1580 | char dsname[MAXNAMELEN]; | |
1581 | zvol_task_t *task; | |
1582 | uint64_t volmode; | |
1583 | ||
1584 | dsl_dataset_name(ds, dsname); | |
1585 | if (dsl_prop_get_int_ds(ds, "volmode", &volmode) != 0) | |
1586 | return (0); | |
1587 | task = zvol_task_alloc(ZVOL_ASYNC_SET_VOLMODE, dsname, NULL, volmode); | |
1588 | if (task == NULL) | |
1589 | return (0); | |
1590 | ||
1591 | (void) taskq_dispatch(dp->dp_spa->spa_zvol_taskq, zvol_task_cb, | |
1592 | task, TQ_SLEEP); | |
1593 | return (0); | |
1594 | } | |
1595 | ||
1596 | /* | |
1597 | * Traverse all child datasets and apply volmode appropriately. | |
1598 | * We call dsl_prop_set_sync_impl() here to set the value only on the toplevel | |
1599 | * dataset and read the effective "volmode" on every child in the callback | |
1600 | * function: this is because the value is not guaranteed to be the same in the | |
1601 | * whole dataset hierarchy. | |
1602 | */ | |
1603 | static void | |
1604 | zvol_set_volmode_sync(void *arg, dmu_tx_t *tx) | |
1605 | { | |
1606 | zvol_set_prop_int_arg_t *zsda = arg; | |
1607 | dsl_pool_t *dp = dmu_tx_pool(tx); | |
1608 | dsl_dir_t *dd; | |
1609 | dsl_dataset_t *ds; | |
1610 | int error; | |
1611 | ||
1612 | VERIFY0(dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL)); | |
1613 | zsda->zsda_tx = tx; | |
1614 | ||
1615 | error = dsl_dataset_hold(dp, zsda->zsda_name, FTAG, &ds); | |
1616 | if (error == 0) { | |
1617 | dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_VOLMODE), | |
1618 | zsda->zsda_source, sizeof (zsda->zsda_value), 1, | |
1619 | &zsda->zsda_value, zsda->zsda_tx); | |
1620 | dsl_dataset_rele(ds, FTAG); | |
1621 | } | |
1622 | ||
1623 | dmu_objset_find_dp(dp, dd->dd_object, zvol_set_volmode_sync_cb, | |
1624 | zsda, DS_FIND_CHILDREN); | |
1625 | ||
1626 | dsl_dir_rele(dd, FTAG); | |
1627 | } | |
1628 | ||
1629 | int | |
1630 | zvol_set_volmode(const char *ddname, zprop_source_t source, uint64_t volmode) | |
1631 | { | |
1632 | zvol_set_prop_int_arg_t zsda; | |
1633 | ||
1634 | zsda.zsda_name = ddname; | |
1635 | zsda.zsda_source = source; | |
1636 | zsda.zsda_value = volmode; | |
1637 | ||
1638 | return (dsl_sync_task(ddname, zvol_set_volmode_check, | |
1639 | zvol_set_volmode_sync, &zsda, 0, ZFS_SPACE_CHECK_NONE)); | |
1640 | } | |
1641 | ||
a0bd735a BP |
1642 | void |
1643 | zvol_create_minors(spa_t *spa, const char *name, boolean_t async) | |
1644 | { | |
1645 | zvol_task_t *task; | |
1646 | taskqid_t id; | |
1647 | ||
1648 | task = zvol_task_alloc(ZVOL_ASYNC_CREATE_MINORS, name, NULL, ~0ULL); | |
1649 | if (task == NULL) | |
1650 | return; | |
1651 | ||
1652 | id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP); | |
48d3eb40 | 1653 | if ((async == B_FALSE) && (id != TASKQID_INVALID)) |
a0bd735a BP |
1654 | taskq_wait_id(spa->spa_zvol_taskq, id); |
1655 | } | |
1656 | ||
1657 | void | |
1658 | zvol_remove_minors(spa_t *spa, const char *name, boolean_t async) | |
1659 | { | |
1660 | zvol_task_t *task; | |
1661 | taskqid_t id; | |
1662 | ||
1663 | task = zvol_task_alloc(ZVOL_ASYNC_REMOVE_MINORS, name, NULL, ~0ULL); | |
1664 | if (task == NULL) | |
1665 | return; | |
5428dc51 | 1666 | |
a0bd735a | 1667 | id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP); |
48d3eb40 | 1668 | if ((async == B_FALSE) && (id != TASKQID_INVALID)) |
a0bd735a BP |
1669 | taskq_wait_id(spa->spa_zvol_taskq, id); |
1670 | } | |
1671 | ||
1672 | void | |
1673 | zvol_rename_minors(spa_t *spa, const char *name1, const char *name2, | |
1674 | boolean_t async) | |
1675 | { | |
1676 | zvol_task_t *task; | |
1677 | taskqid_t id; | |
1678 | ||
1679 | task = zvol_task_alloc(ZVOL_ASYNC_RENAME_MINORS, name1, name2, ~0ULL); | |
1680 | if (task == NULL) | |
1681 | return; | |
1682 | ||
1683 | id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP); | |
48d3eb40 | 1684 | if ((async == B_FALSE) && (id != TASKQID_INVALID)) |
a0bd735a | 1685 | taskq_wait_id(spa->spa_zvol_taskq, id); |
0b4d1b58 ED |
1686 | } |
1687 | ||
5df7e9d8 MM |
1688 | boolean_t |
1689 | zvol_is_zvol(const char *name) | |
1690 | { | |
1691 | ||
1692 | return (ops->zv_is_zvol(name)); | |
1693 | } | |
1694 | ||
1695 | void | |
1696 | zvol_register_ops(const zvol_platform_ops_t *zvol_ops) | |
1697 | { | |
1698 | ops = zvol_ops; | |
1699 | } | |
1700 | ||
60101509 | 1701 | int |
5df7e9d8 | 1702 | zvol_init_impl(void) |
60101509 | 1703 | { |
5df7e9d8 | 1704 | int i; |
60101509 | 1705 | |
2a3871d4 | 1706 | list_create(&zvol_state_list, sizeof (zvol_state_t), |
ce37ebd2 | 1707 | offsetof(zvol_state_t, zv_next)); |
7b98f0d9 | 1708 | rw_init(&zvol_state_lock, NULL, RW_DEFAULT, NULL); |
692e55b8 | 1709 | |
d45e010d CC |
1710 | zvol_htable = kmem_alloc(ZVOL_HT_SIZE * sizeof (struct hlist_head), |
1711 | KM_SLEEP); | |
d45e010d CC |
1712 | for (i = 0; i < ZVOL_HT_SIZE; i++) |
1713 | INIT_HLIST_HEAD(&zvol_htable[i]); | |
1714 | ||
60101509 BB |
1715 | return (0); |
1716 | } | |
1717 | ||
1718 | void | |
5df7e9d8 | 1719 | zvol_fini_impl(void) |
60101509 | 1720 | { |
d45e010d | 1721 | kmem_free(zvol_htable, ZVOL_HT_SIZE * sizeof (struct hlist_head)); |
60101509 | 1722 | list_destroy(&zvol_state_list); |
7b98f0d9 | 1723 | rw_destroy(&zvol_state_lock); |
60101509 | 1724 | } |