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