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