]>
Commit | Line | Data |
---|---|---|
70e083d2 TG |
1 | /* |
2 | * CDDL HEADER START | |
3 | * | |
4 | * The contents of this file are subject to the terms of the | |
5 | * Common Development and Distribution License (the "License"). | |
6 | * You may not use this file except in compliance with the License. | |
7 | * | |
8 | * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE | |
9 | * or http://www.opensolaris.org/os/licensing. | |
10 | * See the License for the specific language governing permissions | |
11 | * and limitations under the License. | |
12 | * | |
13 | * When distributing Covered Code, include this CDDL HEADER in each | |
14 | * file and include the License file at usr/src/OPENSOLARIS.LICENSE. | |
15 | * If applicable, add the following below this CDDL HEADER, with the | |
16 | * fields enclosed by brackets "[]" replaced with your own identifying | |
17 | * information: Portions Copyright [yyyy] [name of copyright owner] | |
18 | * | |
19 | * CDDL HEADER END | |
20 | */ | |
21 | /* | |
22 | * Copyright (C) 2008-2010 Lawrence Livermore National Security, LLC. | |
23 | * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER). | |
24 | * Rewritten for Linux by Brian Behlendorf <behlendorf1@llnl.gov>. | |
25 | * LLNL-CODE-403049. | |
26 | * | |
27 | * ZFS volume emulation driver. | |
28 | * | |
29 | * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes. | |
30 | * Volumes are accessed through the symbolic links named: | |
31 | * | |
32 | * /dev/<pool_name>/<dataset_name> | |
33 | * | |
34 | * Volumes are persistent through reboot and module load. No user command | |
35 | * needs to be run before opening and using a device. | |
36 | * | |
37 | * Copyright (c) 2016 Actifio, Inc. All rights reserved. | |
38 | */ | |
39 | ||
40 | #include <sys/dbuf.h> | |
41 | #include <sys/dmu_traverse.h> | |
42 | #include <sys/dsl_dataset.h> | |
43 | #include <sys/dsl_prop.h> | |
44 | #include <sys/dsl_dir.h> | |
45 | #include <sys/zap.h> | |
46 | #include <sys/zfeature.h> | |
47 | #include <sys/zil_impl.h> | |
48 | #include <sys/dmu_tx.h> | |
49 | #include <sys/zio.h> | |
50 | #include <sys/zfs_rlock.h> | |
51 | #include <sys/zfs_znode.h> | |
52 | #include <sys/spa_impl.h> | |
53 | #include <sys/zvol.h> | |
54 | #include <linux/blkdev_compat.h> | |
93f7b346 | 55 | #include <linux/version.h> |
70e083d2 TG |
56 | |
57 | unsigned int zvol_inhibit_dev = 0; | |
58 | unsigned int zvol_major = ZVOL_MAJOR; | |
59 | unsigned int zvol_prefetch_bytes = (128 * 1024); | |
60 | unsigned long zvol_max_discard_blocks = 16384; | |
61 | ||
62 | static kmutex_t zvol_state_lock; | |
63 | static list_t zvol_state_list; | |
64 | static char *zvol_tag = "zvol_tag"; | |
65 | ||
66 | /* | |
67 | * The in-core state of each volume. | |
68 | */ | |
69 | typedef struct zvol_state { | |
70 | char zv_name[MAXNAMELEN]; /* name */ | |
71 | uint64_t zv_volsize; /* advertised space */ | |
72 | uint64_t zv_volblocksize; /* volume block size */ | |
73 | objset_t *zv_objset; /* objset handle */ | |
74 | uint32_t zv_flags; /* ZVOL_* flags */ | |
75 | uint32_t zv_open_count; /* open counts */ | |
76 | uint32_t zv_changed; /* disk changed */ | |
77 | zilog_t *zv_zilog; /* ZIL handle */ | |
78 | zfs_rlock_t zv_range_lock; /* range lock */ | |
79 | dmu_buf_t *zv_dbuf; /* bonus handle */ | |
80 | dev_t zv_dev; /* device id */ | |
81 | struct gendisk *zv_disk; /* generic disk */ | |
82 | struct request_queue *zv_queue; /* request queue */ | |
83 | spinlock_t zv_lock; /* request queue lock */ | |
84 | list_node_t zv_next; /* next zvol_state_t linkage */ | |
85 | } zvol_state_t; | |
86 | ||
87 | typedef enum { | |
88 | ZVOL_ASYNC_CREATE_MINORS, | |
89 | ZVOL_ASYNC_REMOVE_MINORS, | |
90 | ZVOL_ASYNC_RENAME_MINORS, | |
91 | ZVOL_ASYNC_SET_SNAPDEV, | |
92 | ZVOL_ASYNC_MAX | |
93 | } zvol_async_op_t; | |
94 | ||
95 | typedef struct { | |
96 | zvol_async_op_t op; | |
97 | char pool[MAXNAMELEN]; | |
98 | char name1[MAXNAMELEN]; | |
99 | char name2[MAXNAMELEN]; | |
100 | zprop_source_t source; | |
101 | uint64_t snapdev; | |
102 | } zvol_task_t; | |
103 | ||
104 | #define ZVOL_RDONLY 0x1 | |
105 | ||
106 | /* | |
107 | * Find the next available range of ZVOL_MINORS minor numbers. The | |
108 | * zvol_state_list is kept in ascending minor order so we simply need | |
109 | * to scan the list for the first gap in the sequence. This allows us | |
110 | * to recycle minor number as devices are created and removed. | |
111 | */ | |
112 | static int | |
113 | zvol_find_minor(unsigned *minor) | |
114 | { | |
115 | zvol_state_t *zv; | |
116 | ||
117 | *minor = 0; | |
118 | ASSERT(MUTEX_HELD(&zvol_state_lock)); | |
119 | for (zv = list_head(&zvol_state_list); zv != NULL; | |
120 | zv = list_next(&zvol_state_list, zv), *minor += ZVOL_MINORS) { | |
121 | if (MINOR(zv->zv_dev) != MINOR(*minor)) | |
122 | break; | |
123 | } | |
124 | ||
125 | /* All minors are in use */ | |
126 | if (*minor >= (1 << MINORBITS)) | |
127 | return (SET_ERROR(ENXIO)); | |
128 | ||
129 | return (0); | |
130 | } | |
131 | ||
132 | /* | |
133 | * Find a zvol_state_t given the full major+minor dev_t. | |
134 | */ | |
135 | static zvol_state_t * | |
136 | zvol_find_by_dev(dev_t dev) | |
137 | { | |
138 | zvol_state_t *zv; | |
139 | ||
140 | ASSERT(MUTEX_HELD(&zvol_state_lock)); | |
141 | for (zv = list_head(&zvol_state_list); zv != NULL; | |
142 | zv = list_next(&zvol_state_list, zv)) { | |
143 | if (zv->zv_dev == dev) | |
144 | return (zv); | |
145 | } | |
146 | ||
147 | return (NULL); | |
148 | } | |
149 | ||
150 | /* | |
151 | * Find a zvol_state_t given the name provided at zvol_alloc() time. | |
152 | */ | |
153 | static zvol_state_t * | |
154 | zvol_find_by_name(const char *name) | |
155 | { | |
156 | zvol_state_t *zv; | |
157 | ||
158 | ASSERT(MUTEX_HELD(&zvol_state_lock)); | |
159 | for (zv = list_head(&zvol_state_list); zv != NULL; | |
160 | zv = list_next(&zvol_state_list, zv)) { | |
161 | if (strncmp(zv->zv_name, name, MAXNAMELEN) == 0) | |
162 | return (zv); | |
163 | } | |
164 | ||
165 | return (NULL); | |
166 | } | |
167 | ||
168 | ||
169 | /* | |
170 | * Given a path, return TRUE if path is a ZVOL. | |
171 | */ | |
172 | boolean_t | |
173 | zvol_is_zvol(const char *device) | |
174 | { | |
175 | struct block_device *bdev; | |
176 | unsigned int major; | |
177 | ||
178 | bdev = vdev_lookup_bdev(device); | |
179 | if (IS_ERR(bdev)) | |
180 | return (B_FALSE); | |
181 | ||
182 | major = MAJOR(bdev->bd_dev); | |
183 | bdput(bdev); | |
184 | ||
185 | if (major == zvol_major) | |
186 | return (B_TRUE); | |
187 | ||
188 | return (B_FALSE); | |
189 | } | |
190 | ||
191 | /* | |
192 | * ZFS_IOC_CREATE callback handles dmu zvol and zap object creation. | |
193 | */ | |
194 | void | |
195 | zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) | |
196 | { | |
197 | zfs_creat_t *zct = arg; | |
198 | nvlist_t *nvprops = zct->zct_props; | |
199 | int error; | |
200 | uint64_t volblocksize, volsize; | |
201 | ||
202 | VERIFY(nvlist_lookup_uint64(nvprops, | |
203 | zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0); | |
204 | if (nvlist_lookup_uint64(nvprops, | |
205 | zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0) | |
206 | volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); | |
207 | ||
208 | /* | |
209 | * These properties must be removed from the list so the generic | |
210 | * property setting step won't apply to them. | |
211 | */ | |
212 | VERIFY(nvlist_remove_all(nvprops, | |
213 | zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0); | |
214 | (void) nvlist_remove_all(nvprops, | |
215 | zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE)); | |
216 | ||
217 | error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize, | |
218 | DMU_OT_NONE, 0, tx); | |
219 | ASSERT(error == 0); | |
220 | ||
221 | error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP, | |
222 | DMU_OT_NONE, 0, tx); | |
223 | ASSERT(error == 0); | |
224 | ||
225 | error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx); | |
226 | ASSERT(error == 0); | |
227 | } | |
228 | ||
229 | /* | |
230 | * ZFS_IOC_OBJSET_STATS entry point. | |
231 | */ | |
232 | int | |
233 | zvol_get_stats(objset_t *os, nvlist_t *nv) | |
234 | { | |
235 | int error; | |
236 | dmu_object_info_t *doi; | |
237 | uint64_t val; | |
238 | ||
239 | error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val); | |
240 | if (error) | |
241 | return (SET_ERROR(error)); | |
242 | ||
243 | dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val); | |
244 | doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP); | |
245 | error = dmu_object_info(os, ZVOL_OBJ, doi); | |
246 | ||
247 | if (error == 0) { | |
248 | dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE, | |
249 | doi->doi_data_block_size); | |
250 | } | |
251 | ||
252 | kmem_free(doi, sizeof (dmu_object_info_t)); | |
253 | ||
254 | return (SET_ERROR(error)); | |
255 | } | |
256 | ||
257 | static void | |
258 | zvol_size_changed(zvol_state_t *zv, uint64_t volsize) | |
259 | { | |
260 | struct block_device *bdev; | |
261 | ||
262 | bdev = bdget_disk(zv->zv_disk, 0); | |
263 | if (bdev == NULL) | |
264 | return; | |
265 | /* | |
266 | * 2.6.28 API change | |
267 | * Added check_disk_size_change() helper function. | |
268 | */ | |
269 | #ifdef HAVE_CHECK_DISK_SIZE_CHANGE | |
270 | set_capacity(zv->zv_disk, volsize >> 9); | |
271 | zv->zv_volsize = volsize; | |
272 | check_disk_size_change(zv->zv_disk, bdev); | |
273 | #else | |
274 | zv->zv_volsize = volsize; | |
275 | zv->zv_changed = 1; | |
276 | (void) check_disk_change(bdev); | |
277 | #endif /* HAVE_CHECK_DISK_SIZE_CHANGE */ | |
278 | ||
279 | bdput(bdev); | |
280 | } | |
281 | ||
282 | /* | |
283 | * Sanity check volume size. | |
284 | */ | |
285 | int | |
286 | zvol_check_volsize(uint64_t volsize, uint64_t blocksize) | |
287 | { | |
288 | if (volsize == 0) | |
289 | return (SET_ERROR(EINVAL)); | |
290 | ||
291 | if (volsize % blocksize != 0) | |
292 | return (SET_ERROR(EINVAL)); | |
293 | ||
294 | #ifdef _ILP32 | |
295 | if (volsize - 1 > MAXOFFSET_T) | |
296 | return (SET_ERROR(EOVERFLOW)); | |
297 | #endif | |
298 | return (0); | |
299 | } | |
300 | ||
301 | /* | |
302 | * Ensure the zap is flushed then inform the VFS of the capacity change. | |
303 | */ | |
304 | static int | |
305 | zvol_update_volsize(uint64_t volsize, objset_t *os) | |
306 | { | |
307 | dmu_tx_t *tx; | |
308 | int error; | |
309 | ||
310 | ASSERT(MUTEX_HELD(&zvol_state_lock)); | |
311 | ||
312 | tx = dmu_tx_create(os); | |
313 | dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); | |
314 | error = dmu_tx_assign(tx, TXG_WAIT); | |
315 | if (error) { | |
316 | dmu_tx_abort(tx); | |
317 | return (SET_ERROR(error)); | |
318 | } | |
319 | ||
320 | error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, | |
321 | &volsize, tx); | |
322 | dmu_tx_commit(tx); | |
323 | ||
324 | if (error == 0) | |
325 | error = dmu_free_long_range(os, | |
326 | ZVOL_OBJ, volsize, DMU_OBJECT_END); | |
327 | ||
328 | return (error); | |
329 | } | |
330 | ||
331 | static int | |
332 | zvol_update_live_volsize(zvol_state_t *zv, uint64_t volsize) | |
333 | { | |
334 | zvol_size_changed(zv, volsize); | |
335 | ||
336 | /* | |
337 | * We should post a event here describing the expansion. However, | |
338 | * the zfs_ereport_post() interface doesn't nicely support posting | |
339 | * events for zvols, it assumes events relate to vdevs or zios. | |
340 | */ | |
341 | ||
342 | return (0); | |
343 | } | |
344 | ||
345 | /* | |
346 | * Set ZFS_PROP_VOLSIZE set entry point. | |
347 | */ | |
348 | int | |
349 | zvol_set_volsize(const char *name, uint64_t volsize) | |
350 | { | |
351 | zvol_state_t *zv = NULL; | |
352 | objset_t *os = NULL; | |
353 | int error; | |
354 | dmu_object_info_t *doi; | |
355 | uint64_t readonly; | |
356 | boolean_t owned = B_FALSE; | |
357 | ||
358 | error = dsl_prop_get_integer(name, | |
359 | zfs_prop_to_name(ZFS_PROP_READONLY), &readonly, NULL); | |
360 | if (error != 0) | |
361 | return (SET_ERROR(error)); | |
362 | if (readonly) | |
363 | return (SET_ERROR(EROFS)); | |
364 | ||
365 | mutex_enter(&zvol_state_lock); | |
366 | zv = zvol_find_by_name(name); | |
367 | ||
368 | if (zv == NULL || zv->zv_objset == NULL) { | |
369 | if ((error = dmu_objset_own(name, DMU_OST_ZVOL, B_FALSE, | |
370 | FTAG, &os)) != 0) { | |
371 | mutex_exit(&zvol_state_lock); | |
372 | return (SET_ERROR(error)); | |
373 | } | |
374 | owned = B_TRUE; | |
375 | if (zv != NULL) | |
376 | zv->zv_objset = os; | |
377 | } else { | |
378 | os = zv->zv_objset; | |
379 | } | |
380 | ||
381 | doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP); | |
382 | ||
383 | if ((error = dmu_object_info(os, ZVOL_OBJ, doi)) || | |
384 | (error = zvol_check_volsize(volsize, doi->doi_data_block_size))) | |
385 | goto out; | |
386 | ||
387 | error = zvol_update_volsize(volsize, os); | |
388 | kmem_free(doi, sizeof (dmu_object_info_t)); | |
389 | ||
390 | if (error == 0 && zv != NULL) | |
391 | error = zvol_update_live_volsize(zv, volsize); | |
392 | out: | |
393 | if (owned) { | |
394 | dmu_objset_disown(os, FTAG); | |
395 | if (zv != NULL) | |
396 | zv->zv_objset = NULL; | |
397 | } | |
398 | mutex_exit(&zvol_state_lock); | |
399 | return (error); | |
400 | } | |
401 | ||
402 | /* | |
403 | * Sanity check volume block size. | |
404 | */ | |
405 | int | |
406 | zvol_check_volblocksize(const char *name, uint64_t volblocksize) | |
407 | { | |
408 | /* Record sizes above 128k need the feature to be enabled */ | |
409 | if (volblocksize > SPA_OLD_MAXBLOCKSIZE) { | |
410 | spa_t *spa; | |
411 | int error; | |
412 | ||
413 | if ((error = spa_open(name, &spa, FTAG)) != 0) | |
414 | return (error); | |
415 | ||
416 | if (!spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) { | |
417 | spa_close(spa, FTAG); | |
418 | return (SET_ERROR(ENOTSUP)); | |
419 | } | |
420 | ||
421 | /* | |
422 | * We don't allow setting the property above 1MB, | |
423 | * unless the tunable has been changed. | |
424 | */ | |
425 | if (volblocksize > zfs_max_recordsize) | |
426 | return (SET_ERROR(EDOM)); | |
427 | ||
428 | spa_close(spa, FTAG); | |
429 | } | |
430 | ||
431 | if (volblocksize < SPA_MINBLOCKSIZE || | |
432 | volblocksize > SPA_MAXBLOCKSIZE || | |
433 | !ISP2(volblocksize)) | |
434 | return (SET_ERROR(EDOM)); | |
435 | ||
436 | return (0); | |
437 | } | |
438 | ||
439 | /* | |
440 | * Set ZFS_PROP_VOLBLOCKSIZE set entry point. | |
441 | */ | |
442 | int | |
443 | zvol_set_volblocksize(const char *name, uint64_t volblocksize) | |
444 | { | |
445 | zvol_state_t *zv; | |
446 | dmu_tx_t *tx; | |
447 | int error; | |
448 | ||
449 | mutex_enter(&zvol_state_lock); | |
450 | ||
451 | zv = zvol_find_by_name(name); | |
452 | if (zv == NULL) { | |
453 | error = SET_ERROR(ENXIO); | |
454 | goto out; | |
455 | } | |
456 | ||
457 | if (zv->zv_flags & ZVOL_RDONLY) { | |
458 | error = SET_ERROR(EROFS); | |
459 | goto out; | |
460 | } | |
461 | ||
462 | tx = dmu_tx_create(zv->zv_objset); | |
463 | dmu_tx_hold_bonus(tx, ZVOL_OBJ); | |
464 | error = dmu_tx_assign(tx, TXG_WAIT); | |
465 | if (error) { | |
466 | dmu_tx_abort(tx); | |
467 | } else { | |
468 | error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ, | |
469 | volblocksize, 0, tx); | |
470 | if (error == ENOTSUP) | |
471 | error = SET_ERROR(EBUSY); | |
472 | dmu_tx_commit(tx); | |
473 | if (error == 0) | |
474 | zv->zv_volblocksize = volblocksize; | |
475 | } | |
476 | out: | |
477 | mutex_exit(&zvol_state_lock); | |
478 | ||
479 | return (SET_ERROR(error)); | |
480 | } | |
481 | ||
482 | /* | |
483 | * Replay a TX_WRITE ZIL transaction that didn't get committed | |
484 | * after a system failure | |
485 | */ | |
486 | static int | |
487 | zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap) | |
488 | { | |
489 | objset_t *os = zv->zv_objset; | |
490 | char *data = (char *)(lr + 1); /* data follows lr_write_t */ | |
491 | uint64_t off = lr->lr_offset; | |
492 | uint64_t len = lr->lr_length; | |
493 | dmu_tx_t *tx; | |
494 | int error; | |
495 | ||
496 | if (byteswap) | |
497 | byteswap_uint64_array(lr, sizeof (*lr)); | |
498 | ||
499 | tx = dmu_tx_create(os); | |
500 | dmu_tx_hold_write(tx, ZVOL_OBJ, off, len); | |
501 | error = dmu_tx_assign(tx, TXG_WAIT); | |
502 | if (error) { | |
503 | dmu_tx_abort(tx); | |
504 | } else { | |
505 | dmu_write(os, ZVOL_OBJ, off, len, data, tx); | |
506 | dmu_tx_commit(tx); | |
507 | } | |
508 | ||
509 | return (SET_ERROR(error)); | |
510 | } | |
511 | ||
512 | static int | |
513 | zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap) | |
514 | { | |
515 | return (SET_ERROR(ENOTSUP)); | |
516 | } | |
517 | ||
518 | /* | |
519 | * Callback vectors for replaying records. | |
520 | * Only TX_WRITE is needed for zvol. | |
521 | */ | |
522 | zil_replay_func_t zvol_replay_vector[TX_MAX_TYPE] = { | |
523 | (zil_replay_func_t)zvol_replay_err, /* no such transaction type */ | |
524 | (zil_replay_func_t)zvol_replay_err, /* TX_CREATE */ | |
525 | (zil_replay_func_t)zvol_replay_err, /* TX_MKDIR */ | |
526 | (zil_replay_func_t)zvol_replay_err, /* TX_MKXATTR */ | |
527 | (zil_replay_func_t)zvol_replay_err, /* TX_SYMLINK */ | |
528 | (zil_replay_func_t)zvol_replay_err, /* TX_REMOVE */ | |
529 | (zil_replay_func_t)zvol_replay_err, /* TX_RMDIR */ | |
530 | (zil_replay_func_t)zvol_replay_err, /* TX_LINK */ | |
531 | (zil_replay_func_t)zvol_replay_err, /* TX_RENAME */ | |
532 | (zil_replay_func_t)zvol_replay_write, /* TX_WRITE */ | |
533 | (zil_replay_func_t)zvol_replay_err, /* TX_TRUNCATE */ | |
534 | (zil_replay_func_t)zvol_replay_err, /* TX_SETATTR */ | |
535 | (zil_replay_func_t)zvol_replay_err, /* TX_ACL */ | |
536 | }; | |
537 | ||
538 | /* | |
539 | * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions. | |
540 | * | |
541 | * We store data in the log buffers if it's small enough. | |
542 | * Otherwise we will later flush the data out via dmu_sync(). | |
543 | */ | |
544 | ssize_t zvol_immediate_write_sz = 32768; | |
545 | ||
546 | static void | |
547 | zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, uint64_t offset, | |
548 | uint64_t size, int sync) | |
549 | { | |
550 | uint32_t blocksize = zv->zv_volblocksize; | |
551 | zilog_t *zilog = zv->zv_zilog; | |
552 | boolean_t slogging; | |
553 | ssize_t immediate_write_sz; | |
554 | ||
555 | if (zil_replaying(zilog, tx)) | |
556 | return; | |
557 | ||
558 | immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT) | |
559 | ? 0 : zvol_immediate_write_sz; | |
560 | slogging = spa_has_slogs(zilog->zl_spa) && | |
561 | (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY); | |
562 | ||
563 | while (size) { | |
564 | itx_t *itx; | |
565 | lr_write_t *lr; | |
566 | ssize_t len; | |
567 | itx_wr_state_t write_state; | |
568 | ||
569 | /* | |
570 | * Unlike zfs_log_write() we can be called with | |
571 | * up to DMU_MAX_ACCESS/2 (5MB) writes. | |
572 | */ | |
573 | if (blocksize > immediate_write_sz && !slogging && | |
574 | size >= blocksize && offset % blocksize == 0) { | |
575 | write_state = WR_INDIRECT; /* uses dmu_sync */ | |
576 | len = blocksize; | |
577 | } else if (sync) { | |
578 | write_state = WR_COPIED; | |
579 | len = MIN(ZIL_MAX_LOG_DATA, size); | |
580 | } else { | |
581 | write_state = WR_NEED_COPY; | |
582 | len = MIN(ZIL_MAX_LOG_DATA, size); | |
583 | } | |
584 | ||
585 | itx = zil_itx_create(TX_WRITE, sizeof (*lr) + | |
586 | (write_state == WR_COPIED ? len : 0)); | |
587 | lr = (lr_write_t *)&itx->itx_lr; | |
588 | if (write_state == WR_COPIED && dmu_read(zv->zv_objset, | |
589 | ZVOL_OBJ, offset, len, lr+1, DMU_READ_NO_PREFETCH) != 0) { | |
590 | zil_itx_destroy(itx); | |
591 | itx = zil_itx_create(TX_WRITE, sizeof (*lr)); | |
592 | lr = (lr_write_t *)&itx->itx_lr; | |
593 | write_state = WR_NEED_COPY; | |
594 | } | |
595 | ||
596 | itx->itx_wr_state = write_state; | |
597 | if (write_state == WR_NEED_COPY) | |
598 | itx->itx_sod += len; | |
599 | lr->lr_foid = ZVOL_OBJ; | |
600 | lr->lr_offset = offset; | |
601 | lr->lr_length = len; | |
602 | lr->lr_blkoff = 0; | |
603 | BP_ZERO(&lr->lr_blkptr); | |
604 | ||
605 | itx->itx_private = zv; | |
606 | itx->itx_sync = sync; | |
607 | ||
608 | (void) zil_itx_assign(zilog, itx, tx); | |
609 | ||
610 | offset += len; | |
611 | size -= len; | |
612 | } | |
613 | } | |
614 | ||
615 | static int | |
616 | zvol_write(struct bio *bio) | |
617 | { | |
93f7b346 | 618 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0) |
70e083d2 | 619 | zvol_state_t *zv = bio->bi_bdev->bd_disk->private_data; |
93f7b346 CIK |
620 | #else |
621 | zvol_state_t *zv = bio->bi_disk->private_data; | |
622 | #endif | |
70e083d2 TG |
623 | uint64_t offset = BIO_BI_SECTOR(bio) << 9; |
624 | uint64_t size = BIO_BI_SIZE(bio); | |
625 | int error = 0; | |
626 | dmu_tx_t *tx; | |
627 | rl_t *rl; | |
628 | ||
629 | ASSERT(zv && zv->zv_open_count > 0); | |
630 | ||
631 | if (bio_is_flush(bio)) | |
632 | zil_commit(zv->zv_zilog, ZVOL_OBJ); | |
633 | ||
634 | /* | |
635 | * Some requests are just for flush and nothing else. | |
636 | */ | |
637 | if (size == 0) | |
638 | goto out; | |
639 | ||
640 | rl = zfs_range_lock(&zv->zv_range_lock, offset, size, RL_WRITER); | |
641 | ||
642 | tx = dmu_tx_create(zv->zv_objset); | |
643 | dmu_tx_hold_write(tx, ZVOL_OBJ, offset, size); | |
644 | ||
645 | /* This will only fail for ENOSPC */ | |
646 | error = dmu_tx_assign(tx, TXG_WAIT); | |
647 | if (error) { | |
648 | dmu_tx_abort(tx); | |
649 | zfs_range_unlock(rl); | |
650 | goto out; | |
651 | } | |
652 | ||
653 | error = dmu_write_bio(zv->zv_objset, ZVOL_OBJ, bio, tx); | |
654 | if (error == 0) | |
655 | zvol_log_write(zv, tx, offset, size, | |
656 | !!(bio_is_fua(bio))); | |
657 | ||
658 | dmu_tx_commit(tx); | |
659 | zfs_range_unlock(rl); | |
660 | ||
661 | if ((bio_is_fua(bio)) || | |
662 | zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS) | |
663 | zil_commit(zv->zv_zilog, ZVOL_OBJ); | |
664 | ||
665 | out: | |
666 | return (error); | |
667 | } | |
668 | ||
669 | static int | |
670 | zvol_discard(struct bio *bio) | |
671 | { | |
93f7b346 | 672 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0) |
70e083d2 | 673 | zvol_state_t *zv = bio->bi_bdev->bd_disk->private_data; |
93f7b346 CIK |
674 | #else |
675 | zvol_state_t *zv = bio->bi_disk->private_data; | |
676 | #endif | |
70e083d2 TG |
677 | uint64_t start = BIO_BI_SECTOR(bio) << 9; |
678 | uint64_t size = BIO_BI_SIZE(bio); | |
679 | uint64_t end = start + size; | |
680 | int error; | |
681 | rl_t *rl; | |
682 | ||
683 | ASSERT(zv && zv->zv_open_count > 0); | |
684 | ||
685 | if (end > zv->zv_volsize) | |
686 | return (SET_ERROR(EIO)); | |
687 | ||
688 | /* | |
689 | * Align the request to volume block boundaries when a secure erase is | |
690 | * not required. This will prevent dnode_free_range() from zeroing out | |
691 | * the unaligned parts which is slow (read-modify-write) and useless | |
692 | * since we are not freeing any space by doing so. | |
693 | */ | |
694 | if (!bio_is_secure_erase(bio)) { | |
695 | start = P2ROUNDUP(start, zv->zv_volblocksize); | |
696 | end = P2ALIGN(end, zv->zv_volblocksize); | |
697 | size = end - start; | |
698 | } | |
699 | ||
700 | if (start >= end) | |
701 | return (0); | |
702 | ||
703 | rl = zfs_range_lock(&zv->zv_range_lock, start, size, RL_WRITER); | |
704 | ||
705 | error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, start, size); | |
706 | ||
707 | /* | |
708 | * TODO: maybe we should add the operation to the log. | |
709 | */ | |
710 | zfs_range_unlock(rl); | |
711 | ||
712 | return (error); | |
713 | } | |
714 | ||
715 | static int | |
716 | zvol_read(struct bio *bio) | |
717 | { | |
93f7b346 | 718 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0) |
70e083d2 | 719 | zvol_state_t *zv = bio->bi_bdev->bd_disk->private_data; |
93f7b346 CIK |
720 | #else |
721 | zvol_state_t *zv = bio->bi_disk->private_data; | |
722 | #endif | |
70e083d2 TG |
723 | uint64_t offset = BIO_BI_SECTOR(bio) << 9; |
724 | uint64_t len = BIO_BI_SIZE(bio); | |
725 | int error; | |
726 | rl_t *rl; | |
727 | ||
728 | ASSERT(zv && zv->zv_open_count > 0); | |
729 | ||
730 | if (len == 0) | |
731 | return (0); | |
732 | ||
733 | rl = zfs_range_lock(&zv->zv_range_lock, offset, len, RL_READER); | |
734 | ||
735 | error = dmu_read_bio(zv->zv_objset, ZVOL_OBJ, bio); | |
736 | ||
737 | zfs_range_unlock(rl); | |
738 | ||
739 | /* convert checksum errors into IO errors */ | |
740 | if (error == ECKSUM) | |
741 | error = SET_ERROR(EIO); | |
742 | ||
743 | return (error); | |
744 | } | |
745 | ||
746 | static MAKE_REQUEST_FN_RET | |
747 | zvol_request(struct request_queue *q, struct bio *bio) | |
748 | { | |
749 | zvol_state_t *zv = q->queuedata; | |
750 | fstrans_cookie_t cookie = spl_fstrans_mark(); | |
751 | uint64_t offset = BIO_BI_SECTOR(bio); | |
752 | unsigned int sectors = bio_sectors(bio); | |
753 | int rw = bio_data_dir(bio); | |
754 | #ifdef HAVE_GENERIC_IO_ACCT | |
755 | unsigned long start = jiffies; | |
756 | #endif | |
757 | int error = 0; | |
758 | ||
759 | if (bio_has_data(bio) && offset + sectors > | |
760 | get_capacity(zv->zv_disk)) { | |
761 | printk(KERN_INFO | |
762 | "%s: bad access: block=%llu, count=%lu\n", | |
763 | zv->zv_disk->disk_name, | |
764 | (long long unsigned)offset, | |
765 | (long unsigned)sectors); | |
766 | error = SET_ERROR(EIO); | |
767 | goto out1; | |
768 | } | |
769 | ||
770 | generic_start_io_acct(rw, sectors, &zv->zv_disk->part0); | |
771 | ||
772 | if (rw == WRITE) { | |
773 | if (unlikely(zv->zv_flags & ZVOL_RDONLY)) { | |
774 | error = SET_ERROR(EROFS); | |
775 | goto out2; | |
776 | } | |
777 | ||
778 | if (bio_is_discard(bio) || bio_is_secure_erase(bio)) { | |
779 | error = zvol_discard(bio); | |
780 | goto out2; | |
781 | } | |
782 | ||
783 | error = zvol_write(bio); | |
784 | } else | |
785 | error = zvol_read(bio); | |
786 | ||
787 | out2: | |
788 | generic_end_io_acct(rw, &zv->zv_disk->part0, start); | |
789 | out1: | |
790 | BIO_END_IO(bio, -error); | |
791 | spl_fstrans_unmark(cookie); | |
792 | #ifdef HAVE_MAKE_REQUEST_FN_RET_INT | |
793 | return (0); | |
794 | #elif defined(HAVE_MAKE_REQUEST_FN_RET_QC) | |
795 | return (BLK_QC_T_NONE); | |
796 | #endif | |
797 | } | |
798 | ||
799 | static void | |
800 | zvol_get_done(zgd_t *zgd, int error) | |
801 | { | |
802 | if (zgd->zgd_db) | |
803 | dmu_buf_rele(zgd->zgd_db, zgd); | |
804 | ||
805 | zfs_range_unlock(zgd->zgd_rl); | |
806 | ||
807 | if (error == 0 && zgd->zgd_bp) | |
808 | zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); | |
809 | ||
810 | kmem_free(zgd, sizeof (zgd_t)); | |
811 | } | |
812 | ||
813 | /* | |
814 | * Get data to generate a TX_WRITE intent log record. | |
815 | */ | |
816 | static int | |
817 | zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) | |
818 | { | |
819 | zvol_state_t *zv = arg; | |
820 | objset_t *os = zv->zv_objset; | |
821 | uint64_t object = ZVOL_OBJ; | |
822 | uint64_t offset = lr->lr_offset; | |
823 | uint64_t size = lr->lr_length; | |
824 | blkptr_t *bp = &lr->lr_blkptr; | |
825 | dmu_buf_t *db; | |
826 | zgd_t *zgd; | |
827 | int error; | |
828 | ||
829 | ASSERT(zio != NULL); | |
830 | ASSERT(size != 0); | |
831 | ||
832 | zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP); | |
833 | zgd->zgd_zilog = zv->zv_zilog; | |
834 | zgd->zgd_rl = zfs_range_lock(&zv->zv_range_lock, offset, size, | |
835 | RL_READER); | |
836 | ||
837 | /* | |
838 | * Write records come in two flavors: immediate and indirect. | |
839 | * For small writes it's cheaper to store the data with the | |
840 | * log record (immediate); for large writes it's cheaper to | |
841 | * sync the data and get a pointer to it (indirect) so that | |
842 | * we don't have to write the data twice. | |
843 | */ | |
844 | if (buf != NULL) { /* immediate write */ | |
845 | error = dmu_read(os, object, offset, size, buf, | |
846 | DMU_READ_NO_PREFETCH); | |
847 | } else { | |
848 | size = zv->zv_volblocksize; | |
849 | offset = P2ALIGN_TYPED(offset, size, uint64_t); | |
850 | error = dmu_buf_hold(os, object, offset, zgd, &db, | |
851 | DMU_READ_NO_PREFETCH); | |
852 | if (error == 0) { | |
853 | blkptr_t *obp = dmu_buf_get_blkptr(db); | |
854 | if (obp) { | |
855 | ASSERT(BP_IS_HOLE(bp)); | |
856 | *bp = *obp; | |
857 | } | |
858 | ||
859 | zgd->zgd_db = db; | |
860 | zgd->zgd_bp = &lr->lr_blkptr; | |
861 | ||
862 | ASSERT(db != NULL); | |
863 | ASSERT(db->db_offset == offset); | |
864 | ASSERT(db->db_size == size); | |
865 | ||
866 | error = dmu_sync(zio, lr->lr_common.lrc_txg, | |
867 | zvol_get_done, zgd); | |
868 | ||
869 | if (error == 0) | |
870 | return (0); | |
871 | } | |
872 | } | |
873 | ||
874 | zvol_get_done(zgd, error); | |
875 | ||
876 | return (SET_ERROR(error)); | |
877 | } | |
878 | ||
879 | /* | |
880 | * The zvol_state_t's are inserted in increasing MINOR(dev_t) order. | |
881 | */ | |
882 | static void | |
883 | zvol_insert(zvol_state_t *zv_insert) | |
884 | { | |
885 | zvol_state_t *zv = NULL; | |
886 | ||
887 | ASSERT(MUTEX_HELD(&zvol_state_lock)); | |
888 | ASSERT3U(MINOR(zv_insert->zv_dev) & ZVOL_MINOR_MASK, ==, 0); | |
889 | for (zv = list_head(&zvol_state_list); zv != NULL; | |
890 | zv = list_next(&zvol_state_list, zv)) { | |
891 | if (MINOR(zv->zv_dev) > MINOR(zv_insert->zv_dev)) | |
892 | break; | |
893 | } | |
894 | ||
895 | list_insert_before(&zvol_state_list, zv, zv_insert); | |
896 | } | |
897 | ||
898 | /* | |
899 | * Simply remove the zvol from to list of zvols. | |
900 | */ | |
901 | static void | |
902 | zvol_remove(zvol_state_t *zv_remove) | |
903 | { | |
904 | ASSERT(MUTEX_HELD(&zvol_state_lock)); | |
905 | list_remove(&zvol_state_list, zv_remove); | |
906 | } | |
907 | ||
908 | static int | |
909 | zvol_first_open(zvol_state_t *zv) | |
910 | { | |
911 | objset_t *os; | |
912 | uint64_t volsize; | |
913 | int error; | |
914 | uint64_t ro; | |
915 | ||
916 | /* lie and say we're read-only */ | |
917 | error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, 1, zvol_tag, &os); | |
918 | if (error) | |
919 | return (SET_ERROR(-error)); | |
920 | ||
921 | zv->zv_objset = os; | |
922 | ||
923 | error = dsl_prop_get_integer(zv->zv_name, "readonly", &ro, NULL); | |
924 | if (error) | |
925 | goto out_owned; | |
926 | ||
927 | error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize); | |
928 | if (error) | |
929 | goto out_owned; | |
930 | ||
931 | error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf); | |
932 | if (error) | |
933 | goto out_owned; | |
934 | ||
935 | set_capacity(zv->zv_disk, volsize >> 9); | |
936 | zv->zv_volsize = volsize; | |
937 | zv->zv_zilog = zil_open(os, zvol_get_data); | |
938 | ||
939 | if (ro || dmu_objset_is_snapshot(os) || | |
940 | !spa_writeable(dmu_objset_spa(os))) { | |
941 | set_disk_ro(zv->zv_disk, 1); | |
942 | zv->zv_flags |= ZVOL_RDONLY; | |
943 | } else { | |
944 | set_disk_ro(zv->zv_disk, 0); | |
945 | zv->zv_flags &= ~ZVOL_RDONLY; | |
946 | } | |
947 | ||
948 | out_owned: | |
949 | if (error) { | |
950 | dmu_objset_disown(os, zvol_tag); | |
951 | zv->zv_objset = NULL; | |
952 | } | |
953 | ||
954 | return (SET_ERROR(-error)); | |
955 | } | |
956 | ||
957 | static void | |
958 | zvol_last_close(zvol_state_t *zv) | |
959 | { | |
960 | zil_close(zv->zv_zilog); | |
961 | zv->zv_zilog = NULL; | |
962 | ||
963 | dmu_buf_rele(zv->zv_dbuf, zvol_tag); | |
964 | zv->zv_dbuf = NULL; | |
965 | ||
966 | /* | |
967 | * Evict cached data | |
968 | */ | |
969 | if (dsl_dataset_is_dirty(dmu_objset_ds(zv->zv_objset)) && | |
970 | !(zv->zv_flags & ZVOL_RDONLY)) | |
971 | txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0); | |
972 | (void) dmu_objset_evict_dbufs(zv->zv_objset); | |
973 | ||
974 | dmu_objset_disown(zv->zv_objset, zvol_tag); | |
975 | zv->zv_objset = NULL; | |
976 | } | |
977 | ||
978 | static int | |
979 | zvol_open(struct block_device *bdev, fmode_t flag) | |
980 | { | |
981 | zvol_state_t *zv; | |
982 | int error = 0, drop_mutex = 0; | |
983 | ||
984 | /* | |
985 | * If the caller is already holding the mutex do not take it | |
986 | * again, this will happen as part of zvol_create_minor_impl(). | |
987 | * Once add_disk() is called the device is live and the kernel | |
988 | * will attempt to open it to read the partition information. | |
989 | */ | |
990 | if (!mutex_owned(&zvol_state_lock)) { | |
991 | mutex_enter(&zvol_state_lock); | |
992 | drop_mutex = 1; | |
993 | } | |
994 | ||
995 | /* | |
996 | * Obtain a copy of private_data under the lock to make sure | |
997 | * that either the result of zvol_freeg() setting | |
998 | * bdev->bd_disk->private_data to NULL is observed, or zvol_free() | |
999 | * is not called on this zv because of the positive zv_open_count. | |
1000 | */ | |
1001 | zv = bdev->bd_disk->private_data; | |
1002 | if (zv == NULL) { | |
1003 | error = -ENXIO; | |
1004 | goto out_mutex; | |
1005 | } | |
1006 | ||
1007 | if (zv->zv_open_count == 0) { | |
1008 | error = zvol_first_open(zv); | |
1009 | if (error) | |
1010 | goto out_mutex; | |
1011 | } | |
1012 | ||
1013 | if ((flag & FMODE_WRITE) && (zv->zv_flags & ZVOL_RDONLY)) { | |
1014 | error = -EROFS; | |
1015 | goto out_open_count; | |
1016 | } | |
1017 | ||
1018 | zv->zv_open_count++; | |
1019 | ||
1020 | check_disk_change(bdev); | |
1021 | ||
1022 | out_open_count: | |
1023 | if (zv->zv_open_count == 0) | |
1024 | zvol_last_close(zv); | |
1025 | ||
1026 | out_mutex: | |
1027 | if (drop_mutex) | |
1028 | mutex_exit(&zvol_state_lock); | |
1029 | ||
1030 | return (SET_ERROR(error)); | |
1031 | } | |
1032 | ||
1033 | #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID | |
1034 | static void | |
1035 | #else | |
1036 | static int | |
1037 | #endif | |
1038 | zvol_release(struct gendisk *disk, fmode_t mode) | |
1039 | { | |
1040 | zvol_state_t *zv = disk->private_data; | |
1041 | int drop_mutex = 0; | |
1042 | ||
1043 | ASSERT(zv && zv->zv_open_count > 0); | |
1044 | ||
1045 | if (!mutex_owned(&zvol_state_lock)) { | |
1046 | mutex_enter(&zvol_state_lock); | |
1047 | drop_mutex = 1; | |
1048 | } | |
1049 | ||
1050 | zv->zv_open_count--; | |
1051 | if (zv->zv_open_count == 0) | |
1052 | zvol_last_close(zv); | |
1053 | ||
1054 | if (drop_mutex) | |
1055 | mutex_exit(&zvol_state_lock); | |
1056 | ||
1057 | #ifndef HAVE_BLOCK_DEVICE_OPERATIONS_RELEASE_VOID | |
1058 | return (0); | |
1059 | #endif | |
1060 | } | |
1061 | ||
1062 | static int | |
1063 | zvol_ioctl(struct block_device *bdev, fmode_t mode, | |
1064 | unsigned int cmd, unsigned long arg) | |
1065 | { | |
1066 | zvol_state_t *zv = bdev->bd_disk->private_data; | |
1067 | int error = 0; | |
1068 | ||
1069 | ASSERT(zv && zv->zv_open_count > 0); | |
1070 | ||
1071 | switch (cmd) { | |
1072 | case BLKFLSBUF: | |
1073 | zil_commit(zv->zv_zilog, ZVOL_OBJ); | |
1074 | break; | |
1075 | case BLKZNAME: | |
1076 | error = copy_to_user((void *)arg, zv->zv_name, MAXNAMELEN); | |
1077 | break; | |
1078 | ||
1079 | default: | |
1080 | error = -ENOTTY; | |
1081 | break; | |
1082 | ||
1083 | } | |
1084 | ||
1085 | return (SET_ERROR(error)); | |
1086 | } | |
1087 | ||
1088 | #ifdef CONFIG_COMPAT | |
1089 | static int | |
1090 | zvol_compat_ioctl(struct block_device *bdev, fmode_t mode, | |
1091 | unsigned cmd, unsigned long arg) | |
1092 | { | |
1093 | return (zvol_ioctl(bdev, mode, cmd, arg)); | |
1094 | } | |
1095 | #else | |
1096 | #define zvol_compat_ioctl NULL | |
1097 | #endif | |
1098 | ||
1099 | static int zvol_media_changed(struct gendisk *disk) | |
1100 | { | |
1101 | zvol_state_t *zv = disk->private_data; | |
1102 | ||
1103 | ASSERT(zv && zv->zv_open_count > 0); | |
1104 | ||
1105 | return (zv->zv_changed); | |
1106 | } | |
1107 | ||
1108 | static int zvol_revalidate_disk(struct gendisk *disk) | |
1109 | { | |
1110 | zvol_state_t *zv = disk->private_data; | |
1111 | ||
1112 | ASSERT(zv && zv->zv_open_count > 0); | |
1113 | ||
1114 | zv->zv_changed = 0; | |
1115 | set_capacity(zv->zv_disk, zv->zv_volsize >> 9); | |
1116 | ||
1117 | return (0); | |
1118 | } | |
1119 | ||
1120 | /* | |
1121 | * Provide a simple virtual geometry for legacy compatibility. For devices | |
1122 | * smaller than 1 MiB a small head and sector count is used to allow very | |
1123 | * tiny devices. For devices over 1 Mib a standard head and sector count | |
1124 | * is used to keep the cylinders count reasonable. | |
1125 | */ | |
1126 | static int | |
1127 | zvol_getgeo(struct block_device *bdev, struct hd_geometry *geo) | |
1128 | { | |
1129 | zvol_state_t *zv = bdev->bd_disk->private_data; | |
1130 | sector_t sectors; | |
1131 | ||
1132 | ASSERT(zv && zv->zv_open_count > 0); | |
1133 | ||
1134 | sectors = get_capacity(zv->zv_disk); | |
1135 | ||
1136 | if (sectors > 2048) { | |
1137 | geo->heads = 16; | |
1138 | geo->sectors = 63; | |
1139 | } else { | |
1140 | geo->heads = 2; | |
1141 | geo->sectors = 4; | |
1142 | } | |
1143 | ||
1144 | geo->start = 0; | |
1145 | geo->cylinders = sectors / (geo->heads * geo->sectors); | |
1146 | ||
1147 | return (0); | |
1148 | } | |
1149 | ||
1150 | static struct kobject * | |
1151 | zvol_probe(dev_t dev, int *part, void *arg) | |
1152 | { | |
1153 | zvol_state_t *zv; | |
1154 | struct kobject *kobj; | |
1155 | ||
1156 | mutex_enter(&zvol_state_lock); | |
1157 | zv = zvol_find_by_dev(dev); | |
1158 | kobj = zv ? get_disk(zv->zv_disk) : NULL; | |
1159 | mutex_exit(&zvol_state_lock); | |
1160 | ||
1161 | return (kobj); | |
1162 | } | |
1163 | ||
1164 | #ifdef HAVE_BDEV_BLOCK_DEVICE_OPERATIONS | |
1165 | static struct block_device_operations zvol_ops = { | |
1166 | .open = zvol_open, | |
1167 | .release = zvol_release, | |
1168 | .ioctl = zvol_ioctl, | |
1169 | .compat_ioctl = zvol_compat_ioctl, | |
1170 | .media_changed = zvol_media_changed, | |
1171 | .revalidate_disk = zvol_revalidate_disk, | |
1172 | .getgeo = zvol_getgeo, | |
1173 | .owner = THIS_MODULE, | |
1174 | }; | |
1175 | ||
1176 | #else /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */ | |
1177 | ||
1178 | static int | |
1179 | zvol_open_by_inode(struct inode *inode, struct file *file) | |
1180 | { | |
1181 | return (zvol_open(inode->i_bdev, file->f_mode)); | |
1182 | } | |
1183 | ||
1184 | static int | |
1185 | zvol_release_by_inode(struct inode *inode, struct file *file) | |
1186 | { | |
1187 | return (zvol_release(inode->i_bdev->bd_disk, file->f_mode)); | |
1188 | } | |
1189 | ||
1190 | static int | |
1191 | zvol_ioctl_by_inode(struct inode *inode, struct file *file, | |
1192 | unsigned int cmd, unsigned long arg) | |
1193 | { | |
1194 | if (file == NULL || inode == NULL) | |
1195 | return (SET_ERROR(-EINVAL)); | |
1196 | ||
1197 | return (zvol_ioctl(inode->i_bdev, file->f_mode, cmd, arg)); | |
1198 | } | |
1199 | ||
1200 | #ifdef CONFIG_COMPAT | |
1201 | static long | |
1202 | zvol_compat_ioctl_by_inode(struct file *file, | |
1203 | unsigned int cmd, unsigned long arg) | |
1204 | { | |
1205 | if (file == NULL) | |
1206 | return (SET_ERROR(-EINVAL)); | |
1207 | ||
1208 | return (zvol_compat_ioctl(file->f_dentry->d_inode->i_bdev, | |
1209 | file->f_mode, cmd, arg)); | |
1210 | } | |
1211 | #else | |
1212 | #define zvol_compat_ioctl_by_inode NULL | |
1213 | #endif | |
1214 | ||
1215 | static struct block_device_operations zvol_ops = { | |
1216 | .open = zvol_open_by_inode, | |
1217 | .release = zvol_release_by_inode, | |
1218 | .ioctl = zvol_ioctl_by_inode, | |
1219 | .compat_ioctl = zvol_compat_ioctl_by_inode, | |
1220 | .media_changed = zvol_media_changed, | |
1221 | .revalidate_disk = zvol_revalidate_disk, | |
1222 | .getgeo = zvol_getgeo, | |
1223 | .owner = THIS_MODULE, | |
1224 | }; | |
1225 | #endif /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */ | |
1226 | ||
1227 | /* | |
1228 | * Allocate memory for a new zvol_state_t and setup the required | |
1229 | * request queue and generic disk structures for the block device. | |
1230 | */ | |
1231 | static zvol_state_t * | |
1232 | zvol_alloc(dev_t dev, const char *name) | |
1233 | { | |
1234 | zvol_state_t *zv; | |
1235 | ||
1236 | zv = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP); | |
1237 | ||
1238 | spin_lock_init(&zv->zv_lock); | |
1239 | list_link_init(&zv->zv_next); | |
1240 | ||
1241 | zv->zv_queue = blk_alloc_queue(GFP_ATOMIC); | |
1242 | if (zv->zv_queue == NULL) | |
1243 | goto out_kmem; | |
1244 | ||
1245 | blk_queue_make_request(zv->zv_queue, zvol_request); | |
1246 | blk_queue_set_write_cache(zv->zv_queue, B_TRUE, B_TRUE); | |
1247 | ||
1248 | zv->zv_disk = alloc_disk(ZVOL_MINORS); | |
1249 | if (zv->zv_disk == NULL) | |
1250 | goto out_queue; | |
1251 | ||
1252 | zv->zv_queue->queuedata = zv; | |
1253 | zv->zv_dev = dev; | |
1254 | zv->zv_open_count = 0; | |
1255 | strlcpy(zv->zv_name, name, MAXNAMELEN); | |
1256 | ||
1257 | zfs_rlock_init(&zv->zv_range_lock); | |
1258 | ||
1259 | zv->zv_disk->major = zvol_major; | |
1260 | zv->zv_disk->first_minor = (dev & MINORMASK); | |
1261 | zv->zv_disk->fops = &zvol_ops; | |
1262 | zv->zv_disk->private_data = zv; | |
1263 | zv->zv_disk->queue = zv->zv_queue; | |
1264 | snprintf(zv->zv_disk->disk_name, DISK_NAME_LEN, "%s%d", | |
1265 | ZVOL_DEV_NAME, (dev & MINORMASK)); | |
1266 | ||
1267 | return (zv); | |
1268 | ||
1269 | out_queue: | |
1270 | blk_cleanup_queue(zv->zv_queue); | |
1271 | out_kmem: | |
1272 | kmem_free(zv, sizeof (zvol_state_t)); | |
1273 | ||
1274 | return (NULL); | |
1275 | } | |
1276 | ||
1277 | /* | |
1278 | * Cleanup then free a zvol_state_t which was created by zvol_alloc(). | |
1279 | */ | |
1280 | static void | |
1281 | zvol_free(zvol_state_t *zv) | |
1282 | { | |
1283 | ASSERT(MUTEX_HELD(&zvol_state_lock)); | |
1284 | ASSERT(zv->zv_open_count == 0); | |
1285 | ||
1286 | zfs_rlock_destroy(&zv->zv_range_lock); | |
1287 | ||
1288 | zv->zv_disk->private_data = NULL; | |
1289 | ||
1290 | del_gendisk(zv->zv_disk); | |
1291 | blk_cleanup_queue(zv->zv_queue); | |
1292 | put_disk(zv->zv_disk); | |
1293 | ||
1294 | kmem_free(zv, sizeof (zvol_state_t)); | |
1295 | } | |
1296 | ||
1297 | /* | |
1298 | * Create a block device minor node and setup the linkage between it | |
1299 | * and the specified volume. Once this function returns the block | |
1300 | * device is live and ready for use. | |
1301 | */ | |
1302 | static int | |
1303 | zvol_create_minor_impl(const char *name) | |
1304 | { | |
1305 | zvol_state_t *zv; | |
1306 | objset_t *os; | |
1307 | dmu_object_info_t *doi; | |
1308 | uint64_t volsize; | |
1309 | uint64_t len; | |
1310 | unsigned minor = 0; | |
1311 | int error = 0; | |
1312 | ||
1313 | mutex_enter(&zvol_state_lock); | |
1314 | ||
1315 | zv = zvol_find_by_name(name); | |
1316 | if (zv) { | |
1317 | error = SET_ERROR(EEXIST); | |
1318 | goto out; | |
1319 | } | |
1320 | ||
1321 | doi = kmem_alloc(sizeof (dmu_object_info_t), KM_SLEEP); | |
1322 | ||
1323 | error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os); | |
1324 | if (error) | |
1325 | goto out_doi; | |
1326 | ||
1327 | error = dmu_object_info(os, ZVOL_OBJ, doi); | |
1328 | if (error) | |
1329 | goto out_dmu_objset_disown; | |
1330 | ||
1331 | error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize); | |
1332 | if (error) | |
1333 | goto out_dmu_objset_disown; | |
1334 | ||
1335 | error = zvol_find_minor(&minor); | |
1336 | if (error) | |
1337 | goto out_dmu_objset_disown; | |
1338 | ||
1339 | zv = zvol_alloc(MKDEV(zvol_major, minor), name); | |
1340 | if (zv == NULL) { | |
1341 | error = SET_ERROR(EAGAIN); | |
1342 | goto out_dmu_objset_disown; | |
1343 | } | |
1344 | ||
1345 | if (dmu_objset_is_snapshot(os)) | |
1346 | zv->zv_flags |= ZVOL_RDONLY; | |
1347 | ||
1348 | zv->zv_volblocksize = doi->doi_data_block_size; | |
1349 | zv->zv_volsize = volsize; | |
1350 | zv->zv_objset = os; | |
1351 | ||
1352 | set_capacity(zv->zv_disk, zv->zv_volsize >> 9); | |
1353 | ||
1354 | blk_queue_max_hw_sectors(zv->zv_queue, (DMU_MAX_ACCESS / 4) >> 9); | |
1355 | blk_queue_max_segments(zv->zv_queue, UINT16_MAX); | |
1356 | blk_queue_max_segment_size(zv->zv_queue, UINT_MAX); | |
1357 | blk_queue_physical_block_size(zv->zv_queue, zv->zv_volblocksize); | |
1358 | blk_queue_io_opt(zv->zv_queue, zv->zv_volblocksize); | |
1359 | blk_queue_max_discard_sectors(zv->zv_queue, | |
1360 | (zvol_max_discard_blocks * zv->zv_volblocksize) >> 9); | |
1361 | blk_queue_discard_granularity(zv->zv_queue, zv->zv_volblocksize); | |
1362 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, zv->zv_queue); | |
1363 | #ifdef QUEUE_FLAG_NONROT | |
1364 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zv->zv_queue); | |
1365 | #endif | |
1366 | #ifdef QUEUE_FLAG_ADD_RANDOM | |
1367 | queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, zv->zv_queue); | |
1368 | #endif | |
1369 | ||
1370 | if (spa_writeable(dmu_objset_spa(os))) { | |
1371 | if (zil_replay_disable) | |
1372 | zil_destroy(dmu_objset_zil(os), B_FALSE); | |
1373 | else | |
1374 | zil_replay(os, zv, zvol_replay_vector); | |
1375 | } | |
1376 | ||
1377 | /* | |
1378 | * When udev detects the addition of the device it will immediately | |
1379 | * invoke blkid(8) to determine the type of content on the device. | |
1380 | * Prefetching the blocks commonly scanned by blkid(8) will speed | |
1381 | * up this process. | |
1382 | */ | |
1383 | len = MIN(MAX(zvol_prefetch_bytes, 0), SPA_MAXBLOCKSIZE); | |
1384 | if (len > 0) { | |
1385 | dmu_prefetch(os, ZVOL_OBJ, 0, len); | |
1386 | dmu_prefetch(os, ZVOL_OBJ, volsize - len, len); | |
1387 | } | |
1388 | ||
1389 | zv->zv_objset = NULL; | |
1390 | out_dmu_objset_disown: | |
1391 | dmu_objset_disown(os, zvol_tag); | |
1392 | out_doi: | |
1393 | kmem_free(doi, sizeof (dmu_object_info_t)); | |
1394 | out: | |
1395 | ||
1396 | if (error == 0) { | |
1397 | zvol_insert(zv); | |
1398 | /* | |
1399 | * Drop the lock to prevent deadlock with sys_open() -> | |
1400 | * zvol_open(), which first takes bd_disk->bd_mutex and then | |
1401 | * takes zvol_state_lock, whereas this code path first takes | |
1402 | * zvol_state_lock, and then takes bd_disk->bd_mutex. | |
1403 | */ | |
1404 | mutex_exit(&zvol_state_lock); | |
1405 | add_disk(zv->zv_disk); | |
1406 | } else { | |
1407 | mutex_exit(&zvol_state_lock); | |
1408 | } | |
1409 | ||
1410 | return (SET_ERROR(error)); | |
1411 | } | |
1412 | ||
1413 | /* | |
1414 | * Rename a block device minor mode for the specified volume. | |
1415 | */ | |
1416 | static void | |
1417 | zvol_rename_minor(zvol_state_t *zv, const char *newname) | |
1418 | { | |
1419 | int readonly = get_disk_ro(zv->zv_disk); | |
1420 | ||
1421 | ASSERT(MUTEX_HELD(&zvol_state_lock)); | |
1422 | ||
1423 | strlcpy(zv->zv_name, newname, sizeof (zv->zv_name)); | |
1424 | ||
1425 | /* | |
1426 | * The block device's read-only state is briefly changed causing | |
1427 | * a KOBJ_CHANGE uevent to be issued. This ensures udev detects | |
1428 | * the name change and fixes the symlinks. This does not change | |
1429 | * ZVOL_RDONLY in zv->zv_flags so the actual read-only state never | |
1430 | * changes. This would normally be done using kobject_uevent() but | |
1431 | * that is a GPL-only symbol which is why we need this workaround. | |
1432 | */ | |
1433 | set_disk_ro(zv->zv_disk, !readonly); | |
1434 | set_disk_ro(zv->zv_disk, readonly); | |
1435 | } | |
1436 | ||
1437 | ||
1438 | /* | |
1439 | * Mask errors to continue dmu_objset_find() traversal | |
1440 | */ | |
1441 | static int | |
1442 | zvol_create_snap_minor_cb(const char *dsname, void *arg) | |
1443 | { | |
1444 | const char *name = (const char *)arg; | |
1445 | ||
1446 | ASSERT0(MUTEX_HELD(&spa_namespace_lock)); | |
1447 | ||
1448 | /* skip the designated dataset */ | |
1449 | if (name && strcmp(dsname, name) == 0) | |
1450 | return (0); | |
1451 | ||
1452 | /* at this point, the dsname should name a snapshot */ | |
1453 | if (strchr(dsname, '@') == 0) { | |
1454 | dprintf("zvol_create_snap_minor_cb(): " | |
1455 | "%s is not a shapshot name\n", dsname); | |
1456 | } else { | |
1457 | (void) zvol_create_minor_impl(dsname); | |
1458 | } | |
1459 | ||
1460 | return (0); | |
1461 | } | |
1462 | ||
1463 | /* | |
1464 | * Mask errors to continue dmu_objset_find() traversal | |
1465 | */ | |
1466 | static int | |
1467 | zvol_create_minors_cb(const char *dsname, void *arg) | |
1468 | { | |
1469 | uint64_t snapdev; | |
1470 | int error; | |
1471 | ||
1472 | ASSERT0(MUTEX_HELD(&spa_namespace_lock)); | |
1473 | ||
1474 | error = dsl_prop_get_integer(dsname, "snapdev", &snapdev, NULL); | |
1475 | if (error) | |
1476 | return (0); | |
1477 | ||
1478 | /* | |
1479 | * Given the name and the 'snapdev' property, create device minor nodes | |
1480 | * with the linkages to zvols/snapshots as needed. | |
1481 | * If the name represents a zvol, create a minor node for the zvol, then | |
1482 | * check if its snapshots are 'visible', and if so, iterate over the | |
1483 | * snapshots and create device minor nodes for those. | |
1484 | */ | |
1485 | if (strchr(dsname, '@') == 0) { | |
1486 | /* create minor for the 'dsname' explicitly */ | |
1487 | error = zvol_create_minor_impl(dsname); | |
1488 | if ((error == 0 || error == EEXIST) && | |
1489 | (snapdev == ZFS_SNAPDEV_VISIBLE)) { | |
1490 | fstrans_cookie_t cookie = spl_fstrans_mark(); | |
1491 | /* | |
1492 | * traverse snapshots only, do not traverse children, | |
1493 | * and skip the 'dsname' | |
1494 | */ | |
1495 | error = dmu_objset_find((char *)dsname, | |
1496 | zvol_create_snap_minor_cb, (void *)dsname, | |
1497 | DS_FIND_SNAPSHOTS); | |
1498 | spl_fstrans_unmark(cookie); | |
1499 | } | |
1500 | } else { | |
1501 | dprintf("zvol_create_minors_cb(): %s is not a zvol name\n", | |
1502 | dsname); | |
1503 | } | |
1504 | ||
1505 | return (0); | |
1506 | } | |
1507 | ||
1508 | /* | |
1509 | * Create minors for the specified dataset, including children and snapshots. | |
1510 | * Pay attention to the 'snapdev' property and iterate over the snapshots | |
1511 | * only if they are 'visible'. This approach allows one to assure that the | |
1512 | * snapshot metadata is read from disk only if it is needed. | |
1513 | * | |
1514 | * The name can represent a dataset to be recursively scanned for zvols and | |
1515 | * their snapshots, or a single zvol snapshot. If the name represents a | |
1516 | * dataset, the scan is performed in two nested stages: | |
1517 | * - scan the dataset for zvols, and | |
1518 | * - for each zvol, create a minor node, then check if the zvol's snapshots | |
1519 | * are 'visible', and only then iterate over the snapshots if needed | |
1520 | * | |
1521 | * If the name represents a snapshot, a check is perfromed if the snapshot is | |
1522 | * 'visible' (which also verifies that the parent is a zvol), and if so, | |
1523 | * a minor node for that snapshot is created. | |
1524 | */ | |
1525 | static int | |
1526 | zvol_create_minors_impl(const char *name) | |
1527 | { | |
1528 | int error = 0; | |
1529 | fstrans_cookie_t cookie; | |
1530 | char *atp, *parent; | |
1531 | ||
1532 | if (zvol_inhibit_dev) | |
1533 | return (0); | |
1534 | ||
1535 | parent = kmem_alloc(MAXPATHLEN, KM_SLEEP); | |
1536 | (void) strlcpy(parent, name, MAXPATHLEN); | |
1537 | ||
1538 | if ((atp = strrchr(parent, '@')) != NULL) { | |
1539 | uint64_t snapdev; | |
1540 | ||
1541 | *atp = '\0'; | |
1542 | error = dsl_prop_get_integer(parent, "snapdev", | |
1543 | &snapdev, NULL); | |
1544 | ||
1545 | if (error == 0 && snapdev == ZFS_SNAPDEV_VISIBLE) | |
1546 | error = zvol_create_minor_impl(name); | |
1547 | } else { | |
1548 | cookie = spl_fstrans_mark(); | |
1549 | error = dmu_objset_find(parent, zvol_create_minors_cb, | |
1550 | NULL, DS_FIND_CHILDREN); | |
1551 | spl_fstrans_unmark(cookie); | |
1552 | } | |
1553 | ||
1554 | kmem_free(parent, MAXPATHLEN); | |
1555 | ||
1556 | return (SET_ERROR(error)); | |
1557 | } | |
1558 | ||
1559 | /* | |
1560 | * Remove minors for specified dataset including children and snapshots. | |
1561 | */ | |
1562 | static void | |
1563 | zvol_remove_minors_impl(const char *name) | |
1564 | { | |
1565 | zvol_state_t *zv, *zv_next; | |
1566 | int namelen = ((name) ? strlen(name) : 0); | |
1567 | ||
1568 | if (zvol_inhibit_dev) | |
1569 | return; | |
1570 | ||
1571 | mutex_enter(&zvol_state_lock); | |
1572 | ||
1573 | for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) { | |
1574 | zv_next = list_next(&zvol_state_list, zv); | |
1575 | ||
1576 | if (name == NULL || strcmp(zv->zv_name, name) == 0 || | |
1577 | (strncmp(zv->zv_name, name, namelen) == 0 && | |
1578 | (zv->zv_name[namelen] == '/' || | |
1579 | zv->zv_name[namelen] == '@'))) { | |
1580 | ||
1581 | /* If in use, leave alone */ | |
1582 | if (zv->zv_open_count > 0) | |
1583 | continue; | |
1584 | ||
1585 | zvol_remove(zv); | |
1586 | zvol_free(zv); | |
1587 | } | |
1588 | } | |
1589 | ||
1590 | mutex_exit(&zvol_state_lock); | |
1591 | } | |
1592 | ||
1593 | /* Remove minor for this specific snapshot only */ | |
1594 | static void | |
1595 | zvol_remove_minor_impl(const char *name) | |
1596 | { | |
1597 | zvol_state_t *zv, *zv_next; | |
1598 | ||
1599 | if (zvol_inhibit_dev) | |
1600 | return; | |
1601 | ||
1602 | if (strchr(name, '@') == NULL) | |
1603 | return; | |
1604 | ||
1605 | mutex_enter(&zvol_state_lock); | |
1606 | ||
1607 | for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) { | |
1608 | zv_next = list_next(&zvol_state_list, zv); | |
1609 | ||
1610 | if (strcmp(zv->zv_name, name) == 0) { | |
1611 | /* If in use, leave alone */ | |
1612 | if (zv->zv_open_count > 0) | |
1613 | continue; | |
1614 | zvol_remove(zv); | |
1615 | zvol_free(zv); | |
1616 | break; | |
1617 | } | |
1618 | } | |
1619 | ||
1620 | mutex_exit(&zvol_state_lock); | |
1621 | } | |
1622 | ||
1623 | /* | |
1624 | * Rename minors for specified dataset including children and snapshots. | |
1625 | */ | |
1626 | static void | |
1627 | zvol_rename_minors_impl(const char *oldname, const char *newname) | |
1628 | { | |
1629 | zvol_state_t *zv, *zv_next; | |
1630 | int oldnamelen, newnamelen; | |
70e083d2 TG |
1631 | |
1632 | if (zvol_inhibit_dev) | |
1633 | return; | |
1634 | ||
1635 | oldnamelen = strlen(oldname); | |
1636 | newnamelen = strlen(newname); | |
70e083d2 TG |
1637 | |
1638 | mutex_enter(&zvol_state_lock); | |
1639 | ||
1640 | for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) { | |
1641 | zv_next = list_next(&zvol_state_list, zv); | |
1642 | ||
1643 | /* If in use, leave alone */ | |
1644 | if (zv->zv_open_count > 0) | |
1645 | continue; | |
1646 | ||
1647 | if (strcmp(zv->zv_name, oldname) == 0) { | |
1648 | zvol_rename_minor(zv, newname); | |
1649 | } else if (strncmp(zv->zv_name, oldname, oldnamelen) == 0 && | |
1650 | (zv->zv_name[oldnamelen] == '/' || | |
1651 | zv->zv_name[oldnamelen] == '@')) { | |
43c96d9a | 1652 | char *name = kmem_asprintf("%s%c%s", newname, |
70e083d2 TG |
1653 | zv->zv_name[oldnamelen], |
1654 | zv->zv_name + oldnamelen + 1); | |
1655 | zvol_rename_minor(zv, name); | |
43c96d9a | 1656 | kmem_free(name, strlen(name + 1)); |
70e083d2 TG |
1657 | } |
1658 | } | |
1659 | ||
1660 | mutex_exit(&zvol_state_lock); | |
70e083d2 TG |
1661 | } |
1662 | ||
1663 | typedef struct zvol_snapdev_cb_arg { | |
1664 | uint64_t snapdev; | |
1665 | } zvol_snapdev_cb_arg_t; | |
1666 | ||
1667 | static int | |
1668 | zvol_set_snapdev_cb(const char *dsname, void *param) { | |
1669 | zvol_snapdev_cb_arg_t *arg = param; | |
1670 | ||
1671 | if (strchr(dsname, '@') == NULL) | |
1672 | return (0); | |
1673 | ||
1674 | switch (arg->snapdev) { | |
1675 | case ZFS_SNAPDEV_VISIBLE: | |
1676 | (void) zvol_create_minor_impl(dsname); | |
1677 | break; | |
1678 | case ZFS_SNAPDEV_HIDDEN: | |
1679 | (void) zvol_remove_minor_impl(dsname); | |
1680 | break; | |
1681 | } | |
1682 | ||
1683 | return (0); | |
1684 | } | |
1685 | ||
1686 | static void | |
1687 | zvol_set_snapdev_impl(char *name, uint64_t snapdev) | |
1688 | { | |
1689 | zvol_snapdev_cb_arg_t arg = {snapdev}; | |
1690 | fstrans_cookie_t cookie = spl_fstrans_mark(); | |
1691 | /* | |
1692 | * The zvol_set_snapdev_sync() sets snapdev appropriately | |
1693 | * in the dataset hierarchy. Here, we only scan snapshots. | |
1694 | */ | |
1695 | dmu_objset_find(name, zvol_set_snapdev_cb, &arg, DS_FIND_SNAPSHOTS); | |
1696 | spl_fstrans_unmark(cookie); | |
1697 | } | |
1698 | ||
1699 | static zvol_task_t * | |
1700 | zvol_task_alloc(zvol_async_op_t op, const char *name1, const char *name2, | |
1701 | uint64_t snapdev) | |
1702 | { | |
1703 | zvol_task_t *task; | |
1704 | char *delim; | |
1705 | ||
1706 | /* Never allow tasks on hidden names. */ | |
1707 | if (name1[0] == '$') | |
1708 | return (NULL); | |
1709 | ||
1710 | task = kmem_zalloc(sizeof (zvol_task_t), KM_SLEEP); | |
1711 | task->op = op; | |
1712 | task->snapdev = snapdev; | |
1713 | delim = strchr(name1, '/'); | |
1714 | strlcpy(task->pool, name1, delim ? (delim - name1 + 1) : MAXNAMELEN); | |
1715 | ||
1716 | strlcpy(task->name1, name1, MAXNAMELEN); | |
1717 | if (name2 != NULL) | |
1718 | strlcpy(task->name2, name2, MAXNAMELEN); | |
1719 | ||
1720 | return (task); | |
1721 | } | |
1722 | ||
1723 | static void | |
1724 | zvol_task_free(zvol_task_t *task) | |
1725 | { | |
1726 | kmem_free(task, sizeof (zvol_task_t)); | |
1727 | } | |
1728 | ||
1729 | /* | |
1730 | * The worker thread function performed asynchronously. | |
1731 | */ | |
1732 | static void | |
1733 | zvol_task_cb(void *param) | |
1734 | { | |
1735 | zvol_task_t *task = (zvol_task_t *)param; | |
1736 | ||
1737 | switch (task->op) { | |
1738 | case ZVOL_ASYNC_CREATE_MINORS: | |
1739 | (void) zvol_create_minors_impl(task->name1); | |
1740 | break; | |
1741 | case ZVOL_ASYNC_REMOVE_MINORS: | |
1742 | zvol_remove_minors_impl(task->name1); | |
1743 | break; | |
1744 | case ZVOL_ASYNC_RENAME_MINORS: | |
1745 | zvol_rename_minors_impl(task->name1, task->name2); | |
1746 | break; | |
1747 | case ZVOL_ASYNC_SET_SNAPDEV: | |
1748 | zvol_set_snapdev_impl(task->name1, task->snapdev); | |
1749 | break; | |
1750 | default: | |
1751 | VERIFY(0); | |
1752 | break; | |
1753 | } | |
1754 | ||
1755 | zvol_task_free(task); | |
1756 | } | |
1757 | ||
1758 | typedef struct zvol_set_snapdev_arg { | |
1759 | const char *zsda_name; | |
1760 | uint64_t zsda_value; | |
1761 | zprop_source_t zsda_source; | |
1762 | dmu_tx_t *zsda_tx; | |
1763 | } zvol_set_snapdev_arg_t; | |
1764 | ||
1765 | /* | |
1766 | * Sanity check the dataset for safe use by the sync task. No additional | |
1767 | * conditions are imposed. | |
1768 | */ | |
1769 | static int | |
1770 | zvol_set_snapdev_check(void *arg, dmu_tx_t *tx) | |
1771 | { | |
1772 | zvol_set_snapdev_arg_t *zsda = arg; | |
1773 | dsl_pool_t *dp = dmu_tx_pool(tx); | |
1774 | dsl_dir_t *dd; | |
1775 | int error; | |
1776 | ||
1777 | error = dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL); | |
1778 | if (error != 0) | |
1779 | return (error); | |
1780 | ||
1781 | dsl_dir_rele(dd, FTAG); | |
1782 | ||
1783 | return (error); | |
1784 | } | |
1785 | ||
1786 | static int | |
1787 | zvol_set_snapdev_sync_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) | |
1788 | { | |
1789 | zvol_set_snapdev_arg_t *zsda = arg; | |
1790 | char dsname[MAXNAMELEN]; | |
1791 | zvol_task_t *task; | |
1792 | ||
1793 | dsl_dataset_name(ds, dsname); | |
1794 | dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_SNAPDEV), | |
1795 | zsda->zsda_source, sizeof (zsda->zsda_value), 1, | |
1796 | &zsda->zsda_value, zsda->zsda_tx); | |
1797 | ||
1798 | task = zvol_task_alloc(ZVOL_ASYNC_SET_SNAPDEV, dsname, | |
1799 | NULL, zsda->zsda_value); | |
1800 | if (task == NULL) | |
1801 | return (0); | |
1802 | ||
1803 | (void) taskq_dispatch(dp->dp_spa->spa_zvol_taskq, zvol_task_cb, | |
1804 | task, TQ_SLEEP); | |
1805 | return (0); | |
1806 | } | |
1807 | ||
1808 | /* | |
1809 | * Traverse all child snapshot datasets and apply snapdev appropriately. | |
1810 | */ | |
1811 | static void | |
1812 | zvol_set_snapdev_sync(void *arg, dmu_tx_t *tx) | |
1813 | { | |
1814 | zvol_set_snapdev_arg_t *zsda = arg; | |
1815 | dsl_pool_t *dp = dmu_tx_pool(tx); | |
1816 | dsl_dir_t *dd; | |
1817 | ||
1818 | VERIFY0(dsl_dir_hold(dp, zsda->zsda_name, FTAG, &dd, NULL)); | |
1819 | zsda->zsda_tx = tx; | |
1820 | ||
1821 | dmu_objset_find_dp(dp, dd->dd_object, zvol_set_snapdev_sync_cb, | |
1822 | zsda, DS_FIND_CHILDREN); | |
1823 | ||
1824 | dsl_dir_rele(dd, FTAG); | |
1825 | } | |
1826 | ||
1827 | int | |
1828 | zvol_set_snapdev(const char *ddname, zprop_source_t source, uint64_t snapdev) | |
1829 | { | |
1830 | zvol_set_snapdev_arg_t zsda; | |
1831 | ||
1832 | zsda.zsda_name = ddname; | |
1833 | zsda.zsda_source = source; | |
1834 | zsda.zsda_value = snapdev; | |
1835 | ||
1836 | return (dsl_sync_task(ddname, zvol_set_snapdev_check, | |
1837 | zvol_set_snapdev_sync, &zsda, 0, ZFS_SPACE_CHECK_NONE)); | |
1838 | } | |
1839 | ||
1840 | void | |
1841 | zvol_create_minors(spa_t *spa, const char *name, boolean_t async) | |
1842 | { | |
1843 | zvol_task_t *task; | |
1844 | taskqid_t id; | |
1845 | ||
1846 | task = zvol_task_alloc(ZVOL_ASYNC_CREATE_MINORS, name, NULL, ~0ULL); | |
1847 | if (task == NULL) | |
1848 | return; | |
1849 | ||
1850 | id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP); | |
1851 | if ((async == B_FALSE) && (id != 0)) | |
1852 | taskq_wait_id(spa->spa_zvol_taskq, id); | |
1853 | } | |
1854 | ||
1855 | void | |
1856 | zvol_remove_minors(spa_t *spa, const char *name, boolean_t async) | |
1857 | { | |
1858 | zvol_task_t *task; | |
1859 | taskqid_t id; | |
1860 | ||
1861 | task = zvol_task_alloc(ZVOL_ASYNC_REMOVE_MINORS, name, NULL, ~0ULL); | |
1862 | if (task == NULL) | |
1863 | return; | |
1864 | ||
1865 | id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP); | |
1866 | if ((async == B_FALSE) && (id != 0)) | |
1867 | taskq_wait_id(spa->spa_zvol_taskq, id); | |
1868 | } | |
1869 | ||
1870 | void | |
1871 | zvol_rename_minors(spa_t *spa, const char *name1, const char *name2, | |
1872 | boolean_t async) | |
1873 | { | |
1874 | zvol_task_t *task; | |
1875 | taskqid_t id; | |
1876 | ||
1877 | task = zvol_task_alloc(ZVOL_ASYNC_RENAME_MINORS, name1, name2, ~0ULL); | |
1878 | if (task == NULL) | |
1879 | return; | |
1880 | ||
1881 | id = taskq_dispatch(spa->spa_zvol_taskq, zvol_task_cb, task, TQ_SLEEP); | |
1882 | if ((async == B_FALSE) && (id != 0)) | |
1883 | taskq_wait_id(spa->spa_zvol_taskq, id); | |
1884 | } | |
1885 | ||
1886 | int | |
1887 | zvol_init(void) | |
1888 | { | |
1889 | int error; | |
1890 | ||
1891 | list_create(&zvol_state_list, sizeof (zvol_state_t), | |
1892 | offsetof(zvol_state_t, zv_next)); | |
1893 | mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL); | |
1894 | ||
1895 | error = register_blkdev(zvol_major, ZVOL_DRIVER); | |
1896 | if (error) { | |
1897 | printk(KERN_INFO "ZFS: register_blkdev() failed %d\n", error); | |
1898 | goto out; | |
1899 | } | |
1900 | ||
1901 | blk_register_region(MKDEV(zvol_major, 0), 1UL << MINORBITS, | |
1902 | THIS_MODULE, zvol_probe, NULL, NULL); | |
1903 | ||
1904 | return (0); | |
1905 | ||
1906 | out: | |
1907 | mutex_destroy(&zvol_state_lock); | |
1908 | list_destroy(&zvol_state_list); | |
1909 | ||
1910 | return (SET_ERROR(error)); | |
1911 | } | |
1912 | ||
1913 | void | |
1914 | zvol_fini(void) | |
1915 | { | |
1916 | zvol_remove_minors_impl(NULL); | |
1917 | ||
1918 | blk_unregister_region(MKDEV(zvol_major, 0), 1UL << MINORBITS); | |
1919 | unregister_blkdev(zvol_major, ZVOL_DRIVER); | |
1920 | ||
1921 | list_destroy(&zvol_state_list); | |
1922 | mutex_destroy(&zvol_state_lock); | |
1923 | } | |
1924 | ||
1925 | module_param(zvol_inhibit_dev, uint, 0644); | |
1926 | MODULE_PARM_DESC(zvol_inhibit_dev, "Do not create zvol device nodes"); | |
1927 | ||
1928 | module_param(zvol_major, uint, 0444); | |
1929 | MODULE_PARM_DESC(zvol_major, "Major number for zvol device"); | |
1930 | ||
1931 | module_param(zvol_max_discard_blocks, ulong, 0444); | |
1932 | MODULE_PARM_DESC(zvol_max_discard_blocks, "Max number of blocks to discard"); | |
1933 | ||
1934 | module_param(zvol_prefetch_bytes, uint, 0644); | |
1935 | MODULE_PARM_DESC(zvol_prefetch_bytes, "Prefetch N bytes at zvol start+end"); |