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