]>
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. | |
36 | */ | |
37 | ||
38 | #include <sys/dmu_traverse.h> | |
39 | #include <sys/dsl_dataset.h> | |
40 | #include <sys/dsl_prop.h> | |
41 | #include <sys/zap.h> | |
42 | #include <sys/zil_impl.h> | |
43 | #include <sys/zio.h> | |
44 | #include <sys/zfs_rlock.h> | |
45 | #include <sys/zfs_znode.h> | |
46 | #include <sys/zvol.h> | |
61e90960 | 47 | #include <linux/blkdev_compat.h> |
60101509 BB |
48 | |
49 | unsigned int zvol_major = ZVOL_MAJOR; | |
dde9380a | 50 | unsigned int zvol_threads = 32; |
60101509 BB |
51 | |
52 | static taskq_t *zvol_taskq; | |
53 | static kmutex_t zvol_state_lock; | |
54 | static list_t zvol_state_list; | |
55 | static char *zvol_tag = "zvol_tag"; | |
56 | ||
57 | /* | |
58 | * The in-core state of each volume. | |
59 | */ | |
60 | typedef struct zvol_state { | |
4c0d8e50 | 61 | char zv_name[MAXNAMELEN]; /* name */ |
60101509 BB |
62 | uint64_t zv_volsize; /* advertised space */ |
63 | uint64_t zv_volblocksize;/* volume block size */ | |
64 | objset_t *zv_objset; /* objset handle */ | |
65 | uint32_t zv_flags; /* ZVOL_* flags */ | |
66 | uint32_t zv_open_count; /* open counts */ | |
67 | uint32_t zv_changed; /* disk changed */ | |
68 | zilog_t *zv_zilog; /* ZIL handle */ | |
69 | znode_t zv_znode; /* for range locking */ | |
70 | dmu_buf_t *zv_dbuf; /* bonus handle */ | |
71 | dev_t zv_dev; /* device id */ | |
72 | struct gendisk *zv_disk; /* generic disk */ | |
73 | struct request_queue *zv_queue; /* request queue */ | |
74 | spinlock_t zv_lock; /* request queue lock */ | |
75 | list_node_t zv_next; /* next zvol_state_t linkage */ | |
76 | } zvol_state_t; | |
77 | ||
78 | #define ZVOL_RDONLY 0x1 | |
79 | ||
80 | /* | |
81 | * Find the next available range of ZVOL_MINORS minor numbers. The | |
82 | * zvol_state_list is kept in ascending minor order so we simply need | |
83 | * to scan the list for the first gap in the sequence. This allows us | |
84 | * to recycle minor number as devices are created and removed. | |
85 | */ | |
86 | static int | |
87 | zvol_find_minor(unsigned *minor) | |
88 | { | |
89 | zvol_state_t *zv; | |
90 | ||
91 | *minor = 0; | |
92 | ASSERT(MUTEX_HELD(&zvol_state_lock)); | |
93 | for (zv = list_head(&zvol_state_list); zv != NULL; | |
94 | zv = list_next(&zvol_state_list, zv), *minor += ZVOL_MINORS) { | |
95 | if (MINOR(zv->zv_dev) != MINOR(*minor)) | |
96 | break; | |
97 | } | |
98 | ||
99 | /* All minors are in use */ | |
100 | if (*minor >= (1 << MINORBITS)) | |
101 | return ENXIO; | |
102 | ||
103 | return 0; | |
104 | } | |
105 | ||
106 | /* | |
107 | * Find a zvol_state_t given the full major+minor dev_t. | |
108 | */ | |
109 | static zvol_state_t * | |
110 | zvol_find_by_dev(dev_t dev) | |
111 | { | |
112 | zvol_state_t *zv; | |
113 | ||
114 | ASSERT(MUTEX_HELD(&zvol_state_lock)); | |
115 | for (zv = list_head(&zvol_state_list); zv != NULL; | |
116 | zv = list_next(&zvol_state_list, zv)) { | |
117 | if (zv->zv_dev == dev) | |
118 | return zv; | |
119 | } | |
120 | ||
121 | return NULL; | |
122 | } | |
123 | ||
124 | /* | |
125 | * Find a zvol_state_t given the name provided at zvol_alloc() time. | |
126 | */ | |
127 | static zvol_state_t * | |
128 | zvol_find_by_name(const char *name) | |
129 | { | |
130 | zvol_state_t *zv; | |
131 | ||
132 | ASSERT(MUTEX_HELD(&zvol_state_lock)); | |
133 | for (zv = list_head(&zvol_state_list); zv != NULL; | |
134 | zv = list_next(&zvol_state_list, zv)) { | |
4c0d8e50 | 135 | if (!strncmp(zv->zv_name, name, MAXNAMELEN)) |
60101509 BB |
136 | return zv; |
137 | } | |
138 | ||
139 | return NULL; | |
140 | } | |
141 | ||
142 | /* | |
143 | * ZFS_IOC_CREATE callback handles dmu zvol and zap object creation. | |
144 | */ | |
145 | void | |
146 | zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) | |
147 | { | |
148 | zfs_creat_t *zct = arg; | |
149 | nvlist_t *nvprops = zct->zct_props; | |
150 | int error; | |
151 | uint64_t volblocksize, volsize; | |
152 | ||
153 | VERIFY(nvlist_lookup_uint64(nvprops, | |
154 | zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0); | |
155 | if (nvlist_lookup_uint64(nvprops, | |
156 | zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0) | |
157 | volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); | |
158 | ||
159 | /* | |
160 | * These properties must be removed from the list so the generic | |
161 | * property setting step won't apply to them. | |
162 | */ | |
163 | VERIFY(nvlist_remove_all(nvprops, | |
164 | zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0); | |
165 | (void) nvlist_remove_all(nvprops, | |
166 | zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE)); | |
167 | ||
168 | error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize, | |
169 | DMU_OT_NONE, 0, tx); | |
170 | ASSERT(error == 0); | |
171 | ||
172 | error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP, | |
173 | DMU_OT_NONE, 0, tx); | |
174 | ASSERT(error == 0); | |
175 | ||
176 | error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx); | |
177 | ASSERT(error == 0); | |
178 | } | |
179 | ||
180 | /* | |
181 | * ZFS_IOC_OBJSET_STATS entry point. | |
182 | */ | |
183 | int | |
184 | zvol_get_stats(objset_t *os, nvlist_t *nv) | |
185 | { | |
186 | int error; | |
187 | dmu_object_info_t *doi; | |
188 | uint64_t val; | |
189 | ||
190 | error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val); | |
191 | if (error) | |
192 | return (error); | |
193 | ||
194 | dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val); | |
195 | doi = kmem_alloc(sizeof(dmu_object_info_t), KM_SLEEP); | |
196 | error = dmu_object_info(os, ZVOL_OBJ, doi); | |
197 | ||
198 | if (error == 0) { | |
199 | dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE, | |
200 | doi->doi_data_block_size); | |
201 | } | |
202 | ||
203 | kmem_free(doi, sizeof(dmu_object_info_t)); | |
204 | ||
205 | return (error); | |
206 | } | |
207 | ||
208 | /* | |
209 | * Sanity check volume size. | |
210 | */ | |
211 | int | |
212 | zvol_check_volsize(uint64_t volsize, uint64_t blocksize) | |
213 | { | |
214 | if (volsize == 0) | |
215 | return (EINVAL); | |
216 | ||
217 | if (volsize % blocksize != 0) | |
218 | return (EINVAL); | |
219 | ||
220 | #ifdef _ILP32 | |
221 | if (volsize - 1 > MAXOFFSET_T) | |
222 | return (EOVERFLOW); | |
223 | #endif | |
224 | return (0); | |
225 | } | |
226 | ||
227 | /* | |
228 | * Ensure the zap is flushed then inform the VFS of the capacity change. | |
229 | */ | |
230 | static int | |
df554c14 | 231 | zvol_update_volsize(zvol_state_t *zv, uint64_t volsize, objset_t *os) |
60101509 BB |
232 | { |
233 | struct block_device *bdev; | |
234 | dmu_tx_t *tx; | |
235 | int error; | |
236 | ||
237 | ASSERT(MUTEX_HELD(&zvol_state_lock)); | |
238 | ||
df554c14 | 239 | tx = dmu_tx_create(os); |
60101509 BB |
240 | dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL); |
241 | error = dmu_tx_assign(tx, TXG_WAIT); | |
242 | if (error) { | |
243 | dmu_tx_abort(tx); | |
244 | return (error); | |
245 | } | |
246 | ||
df554c14 | 247 | error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, |
60101509 BB |
248 | &volsize, tx); |
249 | dmu_tx_commit(tx); | |
250 | ||
251 | if (error) | |
252 | return (error); | |
253 | ||
df554c14 | 254 | error = dmu_free_long_range(os, |
60101509 BB |
255 | ZVOL_OBJ, volsize, DMU_OBJECT_END); |
256 | if (error) | |
257 | return (error); | |
258 | ||
60101509 BB |
259 | bdev = bdget_disk(zv->zv_disk, 0); |
260 | if (!bdev) | |
df554c14 BB |
261 | return (EIO); |
262 | /* | |
263 | * 2.6.28 API change | |
264 | * Added check_disk_size_change() helper function. | |
265 | */ | |
266 | #ifdef HAVE_CHECK_DISK_SIZE_CHANGE | |
267 | set_capacity(zv->zv_disk, volsize >> 9); | |
268 | zv->zv_volsize = volsize; | |
269 | check_disk_size_change(zv->zv_disk, bdev); | |
270 | #else | |
271 | zv->zv_volsize = volsize; | |
272 | zv->zv_changed = 1; | |
273 | (void) check_disk_change(bdev); | |
274 | #endif /* HAVE_CHECK_DISK_SIZE_CHANGE */ | |
60101509 | 275 | |
60101509 BB |
276 | bdput(bdev); |
277 | ||
278 | return (0); | |
279 | } | |
280 | ||
281 | /* | |
282 | * Set ZFS_PROP_VOLSIZE set entry point. | |
283 | */ | |
284 | int | |
285 | zvol_set_volsize(const char *name, uint64_t volsize) | |
286 | { | |
287 | zvol_state_t *zv; | |
288 | dmu_object_info_t *doi; | |
289 | objset_t *os = NULL; | |
290 | uint64_t readonly; | |
291 | int error; | |
292 | ||
293 | mutex_enter(&zvol_state_lock); | |
294 | ||
295 | zv = zvol_find_by_name(name); | |
296 | if (zv == NULL) { | |
297 | error = ENXIO; | |
298 | goto out; | |
299 | } | |
300 | ||
301 | doi = kmem_alloc(sizeof(dmu_object_info_t), KM_SLEEP); | |
302 | ||
303 | error = dmu_objset_hold(name, FTAG, &os); | |
304 | if (error) | |
305 | goto out_doi; | |
306 | ||
307 | if ((error = dmu_object_info(os, ZVOL_OBJ, doi)) != 0 || | |
308 | (error = zvol_check_volsize(volsize,doi->doi_data_block_size)) != 0) | |
309 | goto out_doi; | |
310 | ||
311 | VERIFY(dsl_prop_get_integer(name, "readonly", &readonly, NULL) == 0); | |
312 | if (readonly) { | |
313 | error = EROFS; | |
314 | goto out_doi; | |
315 | } | |
316 | ||
317 | if (get_disk_ro(zv->zv_disk) || (zv->zv_flags & ZVOL_RDONLY)) { | |
318 | error = EROFS; | |
319 | goto out_doi; | |
320 | } | |
321 | ||
df554c14 | 322 | error = zvol_update_volsize(zv, volsize, os); |
60101509 BB |
323 | out_doi: |
324 | kmem_free(doi, sizeof(dmu_object_info_t)); | |
325 | out: | |
326 | if (os) | |
327 | dmu_objset_rele(os, FTAG); | |
328 | ||
329 | mutex_exit(&zvol_state_lock); | |
330 | ||
331 | return (error); | |
332 | } | |
333 | ||
334 | /* | |
335 | * Sanity check volume block size. | |
336 | */ | |
337 | int | |
338 | zvol_check_volblocksize(uint64_t volblocksize) | |
339 | { | |
340 | if (volblocksize < SPA_MINBLOCKSIZE || | |
341 | volblocksize > SPA_MAXBLOCKSIZE || | |
342 | !ISP2(volblocksize)) | |
343 | return (EDOM); | |
344 | ||
345 | return (0); | |
346 | } | |
347 | ||
348 | /* | |
349 | * Set ZFS_PROP_VOLBLOCKSIZE set entry point. | |
350 | */ | |
351 | int | |
352 | zvol_set_volblocksize(const char *name, uint64_t volblocksize) | |
353 | { | |
354 | zvol_state_t *zv; | |
355 | dmu_tx_t *tx; | |
356 | int error; | |
357 | ||
358 | mutex_enter(&zvol_state_lock); | |
359 | ||
360 | zv = zvol_find_by_name(name); | |
361 | if (zv == NULL) { | |
362 | error = ENXIO; | |
363 | goto out; | |
364 | } | |
365 | ||
366 | if (get_disk_ro(zv->zv_disk) || (zv->zv_flags & ZVOL_RDONLY)) { | |
367 | error = EROFS; | |
368 | goto out; | |
369 | } | |
370 | ||
371 | tx = dmu_tx_create(zv->zv_objset); | |
372 | dmu_tx_hold_bonus(tx, ZVOL_OBJ); | |
373 | error = dmu_tx_assign(tx, TXG_WAIT); | |
374 | if (error) { | |
375 | dmu_tx_abort(tx); | |
376 | } else { | |
377 | error = dmu_object_set_blocksize(zv->zv_objset, ZVOL_OBJ, | |
378 | volblocksize, 0, tx); | |
379 | if (error == ENOTSUP) | |
380 | error = EBUSY; | |
381 | dmu_tx_commit(tx); | |
382 | if (error == 0) | |
383 | zv->zv_volblocksize = volblocksize; | |
384 | } | |
385 | out: | |
386 | mutex_exit(&zvol_state_lock); | |
387 | ||
388 | return (error); | |
389 | } | |
390 | ||
391 | /* | |
392 | * Replay a TX_WRITE ZIL transaction that didn't get committed | |
393 | * after a system failure | |
394 | */ | |
395 | static int | |
396 | zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap) | |
397 | { | |
398 | objset_t *os = zv->zv_objset; | |
399 | char *data = (char *)(lr + 1); /* data follows lr_write_t */ | |
400 | uint64_t off = lr->lr_offset; | |
401 | uint64_t len = lr->lr_length; | |
402 | dmu_tx_t *tx; | |
403 | int error; | |
404 | ||
405 | if (byteswap) | |
406 | byteswap_uint64_array(lr, sizeof (*lr)); | |
407 | ||
408 | tx = dmu_tx_create(os); | |
409 | dmu_tx_hold_write(tx, ZVOL_OBJ, off, len); | |
410 | error = dmu_tx_assign(tx, TXG_WAIT); | |
411 | if (error) { | |
412 | dmu_tx_abort(tx); | |
413 | } else { | |
414 | dmu_write(os, ZVOL_OBJ, off, len, data, tx); | |
415 | dmu_tx_commit(tx); | |
416 | } | |
417 | ||
418 | return (error); | |
419 | } | |
420 | ||
421 | static int | |
422 | zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap) | |
423 | { | |
424 | return (ENOTSUP); | |
425 | } | |
426 | ||
427 | /* | |
428 | * Callback vectors for replaying records. | |
429 | * Only TX_WRITE is needed for zvol. | |
430 | */ | |
431 | zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = { | |
432 | (zil_replay_func_t *)zvol_replay_err, /* no such transaction type */ | |
433 | (zil_replay_func_t *)zvol_replay_err, /* TX_CREATE */ | |
434 | (zil_replay_func_t *)zvol_replay_err, /* TX_MKDIR */ | |
435 | (zil_replay_func_t *)zvol_replay_err, /* TX_MKXATTR */ | |
436 | (zil_replay_func_t *)zvol_replay_err, /* TX_SYMLINK */ | |
437 | (zil_replay_func_t *)zvol_replay_err, /* TX_REMOVE */ | |
438 | (zil_replay_func_t *)zvol_replay_err, /* TX_RMDIR */ | |
439 | (zil_replay_func_t *)zvol_replay_err, /* TX_LINK */ | |
440 | (zil_replay_func_t *)zvol_replay_err, /* TX_RENAME */ | |
441 | (zil_replay_func_t *)zvol_replay_write, /* TX_WRITE */ | |
442 | (zil_replay_func_t *)zvol_replay_err, /* TX_TRUNCATE */ | |
443 | (zil_replay_func_t *)zvol_replay_err, /* TX_SETATTR */ | |
444 | (zil_replay_func_t *)zvol_replay_err, /* TX_ACL */ | |
445 | }; | |
446 | ||
447 | /* | |
448 | * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions. | |
449 | * | |
450 | * We store data in the log buffers if it's small enough. | |
451 | * Otherwise we will later flush the data out via dmu_sync(). | |
452 | */ | |
453 | ssize_t zvol_immediate_write_sz = 32768; | |
454 | ||
455 | static void | |
456 | zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, | |
457 | uint64_t offset, uint64_t size, int sync) | |
458 | { | |
459 | uint32_t blocksize = zv->zv_volblocksize; | |
460 | zilog_t *zilog = zv->zv_zilog; | |
461 | boolean_t slogging; | |
ab85f845 | 462 | ssize_t immediate_write_sz; |
60101509 BB |
463 | |
464 | if (zil_replaying(zilog, tx)) | |
465 | return; | |
466 | ||
ab85f845 ED |
467 | immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT) |
468 | ? 0 : zvol_immediate_write_sz; | |
469 | slogging = spa_has_slogs(zilog->zl_spa) && | |
470 | (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY); | |
60101509 BB |
471 | |
472 | while (size) { | |
473 | itx_t *itx; | |
474 | lr_write_t *lr; | |
475 | ssize_t len; | |
476 | itx_wr_state_t write_state; | |
477 | ||
478 | /* | |
479 | * Unlike zfs_log_write() we can be called with | |
480 | * up to DMU_MAX_ACCESS/2 (5MB) writes. | |
481 | */ | |
ab85f845 | 482 | if (blocksize > immediate_write_sz && !slogging && |
60101509 BB |
483 | size >= blocksize && offset % blocksize == 0) { |
484 | write_state = WR_INDIRECT; /* uses dmu_sync */ | |
485 | len = blocksize; | |
486 | } else if (sync) { | |
487 | write_state = WR_COPIED; | |
488 | len = MIN(ZIL_MAX_LOG_DATA, size); | |
489 | } else { | |
490 | write_state = WR_NEED_COPY; | |
491 | len = MIN(ZIL_MAX_LOG_DATA, size); | |
492 | } | |
493 | ||
494 | itx = zil_itx_create(TX_WRITE, sizeof (*lr) + | |
495 | (write_state == WR_COPIED ? len : 0)); | |
496 | lr = (lr_write_t *)&itx->itx_lr; | |
497 | if (write_state == WR_COPIED && dmu_read(zv->zv_objset, | |
498 | ZVOL_OBJ, offset, len, lr+1, DMU_READ_NO_PREFETCH) != 0) { | |
499 | zil_itx_destroy(itx); | |
500 | itx = zil_itx_create(TX_WRITE, sizeof (*lr)); | |
501 | lr = (lr_write_t *)&itx->itx_lr; | |
502 | write_state = WR_NEED_COPY; | |
503 | } | |
504 | ||
505 | itx->itx_wr_state = write_state; | |
506 | if (write_state == WR_NEED_COPY) | |
507 | itx->itx_sod += len; | |
508 | lr->lr_foid = ZVOL_OBJ; | |
509 | lr->lr_offset = offset; | |
510 | lr->lr_length = len; | |
511 | lr->lr_blkoff = 0; | |
512 | BP_ZERO(&lr->lr_blkptr); | |
513 | ||
514 | itx->itx_private = zv; | |
515 | itx->itx_sync = sync; | |
516 | ||
517 | (void) zil_itx_assign(zilog, itx, tx); | |
518 | ||
519 | offset += len; | |
520 | size -= len; | |
521 | } | |
522 | } | |
523 | ||
524 | /* | |
525 | * Common write path running under the zvol taskq context. This function | |
526 | * is responsible for copying the request structure data in to the DMU and | |
527 | * signaling the request queue with the result of the copy. | |
528 | */ | |
529 | static void | |
530 | zvol_write(void *arg) | |
531 | { | |
532 | struct request *req = (struct request *)arg; | |
533 | struct request_queue *q = req->q; | |
534 | zvol_state_t *zv = q->queuedata; | |
535 | uint64_t offset = blk_rq_pos(req) << 9; | |
536 | uint64_t size = blk_rq_bytes(req); | |
537 | int error = 0; | |
538 | dmu_tx_t *tx; | |
539 | rl_t *rl; | |
540 | ||
b18019d2 ED |
541 | if (req->cmd_flags & VDEV_REQ_FLUSH) |
542 | zil_commit(zv->zv_zilog, ZVOL_OBJ); | |
543 | ||
544 | /* | |
545 | * Some requests are just for flush and nothing else. | |
546 | */ | |
547 | if (size == 0) { | |
548 | blk_end_request(req, 0, size); | |
549 | return; | |
550 | } | |
551 | ||
60101509 BB |
552 | rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_WRITER); |
553 | ||
554 | tx = dmu_tx_create(zv->zv_objset); | |
555 | dmu_tx_hold_write(tx, ZVOL_OBJ, offset, size); | |
556 | ||
557 | /* This will only fail for ENOSPC */ | |
558 | error = dmu_tx_assign(tx, TXG_WAIT); | |
559 | if (error) { | |
560 | dmu_tx_abort(tx); | |
561 | zfs_range_unlock(rl); | |
562 | blk_end_request(req, -error, size); | |
563 | return; | |
564 | } | |
565 | ||
566 | error = dmu_write_req(zv->zv_objset, ZVOL_OBJ, req, tx); | |
567 | if (error == 0) | |
b18019d2 ED |
568 | zvol_log_write(zv, tx, offset, size, |
569 | req->cmd_flags & VDEV_REQ_FUA); | |
60101509 BB |
570 | |
571 | dmu_tx_commit(tx); | |
572 | zfs_range_unlock(rl); | |
573 | ||
b18019d2 ED |
574 | if ((req->cmd_flags & VDEV_REQ_FUA) || |
575 | zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS) | |
60101509 BB |
576 | zil_commit(zv->zv_zilog, ZVOL_OBJ); |
577 | ||
578 | blk_end_request(req, -error, size); | |
579 | } | |
580 | ||
30930fba ED |
581 | #ifdef HAVE_BLK_QUEUE_DISCARD |
582 | static void | |
583 | zvol_discard(void *arg) | |
584 | { | |
585 | struct request *req = (struct request *)arg; | |
586 | struct request_queue *q = req->q; | |
587 | zvol_state_t *zv = q->queuedata; | |
588 | uint64_t offset = blk_rq_pos(req) << 9; | |
589 | uint64_t size = blk_rq_bytes(req); | |
590 | int error; | |
591 | rl_t *rl; | |
592 | ||
593 | if (offset + size > zv->zv_volsize) { | |
594 | blk_end_request(req, -EIO, size); | |
595 | return; | |
596 | } | |
597 | ||
598 | if (size == 0) { | |
599 | blk_end_request(req, 0, size); | |
600 | return; | |
601 | } | |
602 | ||
603 | rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_WRITER); | |
604 | ||
605 | error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, offset, size); | |
606 | ||
607 | /* | |
608 | * TODO: maybe we should add the operation to the log. | |
609 | */ | |
610 | ||
611 | zfs_range_unlock(rl); | |
612 | ||
613 | blk_end_request(req, -error, size); | |
614 | } | |
615 | #endif /* HAVE_BLK_QUEUE_DISCARD */ | |
616 | ||
60101509 BB |
617 | /* |
618 | * Common read path running under the zvol taskq context. This function | |
619 | * is responsible for copying the requested data out of the DMU and in to | |
620 | * a linux request structure. It then must signal the request queue with | |
621 | * an error code describing the result of the copy. | |
622 | */ | |
623 | static void | |
624 | zvol_read(void *arg) | |
625 | { | |
626 | struct request *req = (struct request *)arg; | |
627 | struct request_queue *q = req->q; | |
628 | zvol_state_t *zv = q->queuedata; | |
629 | uint64_t offset = blk_rq_pos(req) << 9; | |
630 | uint64_t size = blk_rq_bytes(req); | |
631 | int error; | |
632 | rl_t *rl; | |
633 | ||
b18019d2 ED |
634 | if (size == 0) { |
635 | blk_end_request(req, 0, size); | |
636 | return; | |
637 | } | |
638 | ||
60101509 BB |
639 | rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER); |
640 | ||
641 | error = dmu_read_req(zv->zv_objset, ZVOL_OBJ, req); | |
642 | ||
643 | zfs_range_unlock(rl); | |
644 | ||
645 | /* convert checksum errors into IO errors */ | |
646 | if (error == ECKSUM) | |
647 | error = EIO; | |
648 | ||
649 | blk_end_request(req, -error, size); | |
650 | } | |
651 | ||
652 | /* | |
653 | * Request will be added back to the request queue and retried if | |
654 | * it cannot be immediately dispatched to the taskq for handling | |
655 | */ | |
656 | static inline void | |
657 | zvol_dispatch(task_func_t func, struct request *req) | |
658 | { | |
659 | if (!taskq_dispatch(zvol_taskq, func, (void *)req, TQ_NOSLEEP)) | |
660 | blk_requeue_request(req->q, req); | |
661 | } | |
662 | ||
663 | /* | |
664 | * Common request path. Rather than registering a custom make_request() | |
665 | * function we use the generic Linux version. This is done because it allows | |
666 | * us to easily merge read requests which would otherwise we performed | |
667 | * synchronously by the DMU. This is less critical in write case where the | |
668 | * DMU will perform the correct merging within a transaction group. Using | |
669 | * the generic make_request() also let's use leverage the fact that the | |
670 | * elevator with ensure correct ordering in regards to barrior IOs. On | |
671 | * the downside it means that in the write case we end up doing request | |
672 | * merging twice once in the elevator and once in the DMU. | |
673 | * | |
674 | * The request handler is called under a spin lock so all the real work | |
675 | * is handed off to be done in the context of the zvol taskq. This function | |
676 | * simply performs basic request sanity checking and hands off the request. | |
677 | */ | |
678 | static void | |
679 | zvol_request(struct request_queue *q) | |
680 | { | |
681 | zvol_state_t *zv = q->queuedata; | |
682 | struct request *req; | |
683 | unsigned int size; | |
684 | ||
685 | while ((req = blk_fetch_request(q)) != NULL) { | |
686 | size = blk_rq_bytes(req); | |
687 | ||
b18019d2 | 688 | if (size != 0 && blk_rq_pos(req) + blk_rq_sectors(req) > |
60101509 BB |
689 | get_capacity(zv->zv_disk)) { |
690 | printk(KERN_INFO | |
691 | "%s: bad access: block=%llu, count=%lu\n", | |
692 | req->rq_disk->disk_name, | |
693 | (long long unsigned)blk_rq_pos(req), | |
694 | (long unsigned)blk_rq_sectors(req)); | |
695 | __blk_end_request(req, -EIO, size); | |
696 | continue; | |
697 | } | |
698 | ||
699 | if (!blk_fs_request(req)) { | |
700 | printk(KERN_INFO "%s: non-fs cmd\n", | |
701 | req->rq_disk->disk_name); | |
702 | __blk_end_request(req, -EIO, size); | |
703 | continue; | |
704 | } | |
705 | ||
706 | switch (rq_data_dir(req)) { | |
707 | case READ: | |
708 | zvol_dispatch(zvol_read, req); | |
709 | break; | |
710 | case WRITE: | |
711 | if (unlikely(get_disk_ro(zv->zv_disk)) || | |
712 | unlikely(zv->zv_flags & ZVOL_RDONLY)) { | |
713 | __blk_end_request(req, -EROFS, size); | |
714 | break; | |
715 | } | |
716 | ||
30930fba ED |
717 | #ifdef HAVE_BLK_QUEUE_DISCARD |
718 | if (req->cmd_flags & VDEV_REQ_DISCARD) { | |
719 | zvol_dispatch(zvol_discard, req); | |
720 | break; | |
721 | } | |
722 | #endif /* HAVE_BLK_QUEUE_DISCARD */ | |
723 | ||
60101509 BB |
724 | zvol_dispatch(zvol_write, req); |
725 | break; | |
726 | default: | |
727 | printk(KERN_INFO "%s: unknown cmd: %d\n", | |
728 | req->rq_disk->disk_name, (int)rq_data_dir(req)); | |
729 | __blk_end_request(req, -EIO, size); | |
730 | break; | |
731 | } | |
732 | } | |
733 | } | |
734 | ||
735 | static void | |
736 | zvol_get_done(zgd_t *zgd, int error) | |
737 | { | |
738 | if (zgd->zgd_db) | |
739 | dmu_buf_rele(zgd->zgd_db, zgd); | |
740 | ||
741 | zfs_range_unlock(zgd->zgd_rl); | |
742 | ||
743 | if (error == 0 && zgd->zgd_bp) | |
744 | zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); | |
745 | ||
746 | kmem_free(zgd, sizeof (zgd_t)); | |
747 | } | |
748 | ||
749 | /* | |
750 | * Get data to generate a TX_WRITE intent log record. | |
751 | */ | |
752 | static int | |
753 | zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) | |
754 | { | |
755 | zvol_state_t *zv = arg; | |
756 | objset_t *os = zv->zv_objset; | |
757 | uint64_t offset = lr->lr_offset; | |
758 | uint64_t size = lr->lr_length; | |
759 | dmu_buf_t *db; | |
760 | zgd_t *zgd; | |
761 | int error; | |
762 | ||
763 | ASSERT(zio != NULL); | |
764 | ASSERT(size != 0); | |
765 | ||
766 | zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP); | |
767 | zgd->zgd_zilog = zv->zv_zilog; | |
768 | zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER); | |
769 | ||
770 | /* | |
771 | * Write records come in two flavors: immediate and indirect. | |
772 | * For small writes it's cheaper to store the data with the | |
773 | * log record (immediate); for large writes it's cheaper to | |
774 | * sync the data and get a pointer to it (indirect) so that | |
775 | * we don't have to write the data twice. | |
776 | */ | |
777 | if (buf != NULL) { /* immediate write */ | |
778 | error = dmu_read(os, ZVOL_OBJ, offset, size, buf, | |
779 | DMU_READ_NO_PREFETCH); | |
780 | } else { | |
781 | size = zv->zv_volblocksize; | |
782 | offset = P2ALIGN_TYPED(offset, size, uint64_t); | |
783 | error = dmu_buf_hold(os, ZVOL_OBJ, offset, zgd, &db, | |
784 | DMU_READ_NO_PREFETCH); | |
785 | if (error == 0) { | |
786 | zgd->zgd_db = db; | |
787 | zgd->zgd_bp = &lr->lr_blkptr; | |
788 | ||
789 | ASSERT(db != NULL); | |
790 | ASSERT(db->db_offset == offset); | |
791 | ASSERT(db->db_size == size); | |
792 | ||
793 | error = dmu_sync(zio, lr->lr_common.lrc_txg, | |
794 | zvol_get_done, zgd); | |
795 | ||
796 | if (error == 0) | |
797 | return (0); | |
798 | } | |
799 | } | |
800 | ||
801 | zvol_get_done(zgd, error); | |
802 | ||
803 | return (error); | |
804 | } | |
805 | ||
806 | /* | |
807 | * The zvol_state_t's are inserted in increasing MINOR(dev_t) order. | |
808 | */ | |
809 | static void | |
810 | zvol_insert(zvol_state_t *zv_insert) | |
811 | { | |
812 | zvol_state_t *zv = NULL; | |
813 | ||
814 | ASSERT(MUTEX_HELD(&zvol_state_lock)); | |
815 | ASSERT3U(MINOR(zv_insert->zv_dev) & ZVOL_MINOR_MASK, ==, 0); | |
816 | for (zv = list_head(&zvol_state_list); zv != NULL; | |
817 | zv = list_next(&zvol_state_list, zv)) { | |
818 | if (MINOR(zv->zv_dev) > MINOR(zv_insert->zv_dev)) | |
819 | break; | |
820 | } | |
821 | ||
822 | list_insert_before(&zvol_state_list, zv, zv_insert); | |
823 | } | |
824 | ||
825 | /* | |
826 | * Simply remove the zvol from to list of zvols. | |
827 | */ | |
828 | static void | |
829 | zvol_remove(zvol_state_t *zv_remove) | |
830 | { | |
831 | ASSERT(MUTEX_HELD(&zvol_state_lock)); | |
832 | list_remove(&zvol_state_list, zv_remove); | |
833 | } | |
834 | ||
835 | static int | |
836 | zvol_first_open(zvol_state_t *zv) | |
837 | { | |
838 | objset_t *os; | |
839 | uint64_t volsize; | |
840 | int error; | |
841 | uint64_t ro; | |
842 | ||
843 | /* lie and say we're read-only */ | |
844 | error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, 1, zvol_tag, &os); | |
845 | if (error) | |
846 | return (-error); | |
847 | ||
848 | error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize); | |
849 | if (error) { | |
850 | dmu_objset_disown(os, zvol_tag); | |
851 | return (-error); | |
852 | } | |
853 | ||
854 | zv->zv_objset = os; | |
855 | error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf); | |
856 | if (error) { | |
857 | dmu_objset_disown(os, zvol_tag); | |
858 | return (-error); | |
859 | } | |
860 | ||
861 | set_capacity(zv->zv_disk, volsize >> 9); | |
862 | zv->zv_volsize = volsize; | |
863 | zv->zv_zilog = zil_open(os, zvol_get_data); | |
864 | ||
865 | VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &ro, NULL) == 0); | |
866 | if (ro || dmu_objset_is_snapshot(os)) { | |
867 | set_disk_ro(zv->zv_disk, 1); | |
868 | zv->zv_flags |= ZVOL_RDONLY; | |
869 | } else { | |
870 | set_disk_ro(zv->zv_disk, 0); | |
871 | zv->zv_flags &= ~ZVOL_RDONLY; | |
872 | } | |
873 | ||
874 | return (-error); | |
875 | } | |
876 | ||
877 | static void | |
878 | zvol_last_close(zvol_state_t *zv) | |
879 | { | |
880 | zil_close(zv->zv_zilog); | |
881 | zv->zv_zilog = NULL; | |
882 | dmu_buf_rele(zv->zv_dbuf, zvol_tag); | |
883 | zv->zv_dbuf = NULL; | |
884 | dmu_objset_disown(zv->zv_objset, zvol_tag); | |
885 | zv->zv_objset = NULL; | |
886 | } | |
887 | ||
888 | static int | |
889 | zvol_open(struct block_device *bdev, fmode_t flag) | |
890 | { | |
891 | zvol_state_t *zv = bdev->bd_disk->private_data; | |
892 | int error = 0, drop_mutex = 0; | |
893 | ||
894 | /* | |
895 | * If the caller is already holding the mutex do not take it | |
896 | * again, this will happen as part of zvol_create_minor(). | |
897 | * Once add_disk() is called the device is live and the kernel | |
898 | * will attempt to open it to read the partition information. | |
899 | */ | |
900 | if (!mutex_owned(&zvol_state_lock)) { | |
901 | mutex_enter(&zvol_state_lock); | |
902 | drop_mutex = 1; | |
903 | } | |
904 | ||
905 | ASSERT3P(zv, !=, NULL); | |
906 | ||
907 | if (zv->zv_open_count == 0) { | |
908 | error = zvol_first_open(zv); | |
909 | if (error) | |
910 | goto out_mutex; | |
911 | } | |
912 | ||
913 | if ((flag & FMODE_WRITE) && | |
914 | (get_disk_ro(zv->zv_disk) || (zv->zv_flags & ZVOL_RDONLY))) { | |
915 | error = -EROFS; | |
916 | goto out_open_count; | |
917 | } | |
918 | ||
919 | zv->zv_open_count++; | |
920 | ||
921 | out_open_count: | |
922 | if (zv->zv_open_count == 0) | |
923 | zvol_last_close(zv); | |
924 | ||
925 | out_mutex: | |
926 | if (drop_mutex) | |
927 | mutex_exit(&zvol_state_lock); | |
928 | ||
929 | check_disk_change(bdev); | |
930 | ||
931 | return (error); | |
932 | } | |
933 | ||
934 | static int | |
935 | zvol_release(struct gendisk *disk, fmode_t mode) | |
936 | { | |
937 | zvol_state_t *zv = disk->private_data; | |
938 | int drop_mutex = 0; | |
939 | ||
940 | if (!mutex_owned(&zvol_state_lock)) { | |
941 | mutex_enter(&zvol_state_lock); | |
942 | drop_mutex = 1; | |
943 | } | |
944 | ||
945 | ASSERT3P(zv, !=, NULL); | |
946 | ASSERT3U(zv->zv_open_count, >, 0); | |
947 | zv->zv_open_count--; | |
948 | if (zv->zv_open_count == 0) | |
949 | zvol_last_close(zv); | |
950 | ||
951 | if (drop_mutex) | |
952 | mutex_exit(&zvol_state_lock); | |
953 | ||
954 | return (0); | |
955 | } | |
956 | ||
957 | static int | |
958 | zvol_ioctl(struct block_device *bdev, fmode_t mode, | |
959 | unsigned int cmd, unsigned long arg) | |
960 | { | |
961 | zvol_state_t *zv = bdev->bd_disk->private_data; | |
962 | int error = 0; | |
963 | ||
964 | if (zv == NULL) | |
965 | return (-ENXIO); | |
966 | ||
967 | switch (cmd) { | |
968 | case BLKFLSBUF: | |
969 | zil_commit(zv->zv_zilog, ZVOL_OBJ); | |
970 | break; | |
4c0d8e50 FN |
971 | case BLKZNAME: |
972 | error = copy_to_user((void *)arg, zv->zv_name, MAXNAMELEN); | |
973 | break; | |
60101509 BB |
974 | |
975 | default: | |
976 | error = -ENOTTY; | |
977 | break; | |
978 | ||
979 | } | |
980 | ||
981 | return (error); | |
982 | } | |
983 | ||
984 | #ifdef CONFIG_COMPAT | |
985 | static int | |
986 | zvol_compat_ioctl(struct block_device *bdev, fmode_t mode, | |
987 | unsigned cmd, unsigned long arg) | |
988 | { | |
989 | return zvol_ioctl(bdev, mode, cmd, arg); | |
990 | } | |
991 | #else | |
992 | #define zvol_compat_ioctl NULL | |
993 | #endif | |
994 | ||
995 | static int zvol_media_changed(struct gendisk *disk) | |
996 | { | |
997 | zvol_state_t *zv = disk->private_data; | |
998 | ||
999 | return zv->zv_changed; | |
1000 | } | |
1001 | ||
1002 | static int zvol_revalidate_disk(struct gendisk *disk) | |
1003 | { | |
1004 | zvol_state_t *zv = disk->private_data; | |
1005 | ||
1006 | zv->zv_changed = 0; | |
1007 | set_capacity(zv->zv_disk, zv->zv_volsize >> 9); | |
1008 | ||
1009 | return 0; | |
1010 | } | |
1011 | ||
1012 | /* | |
1013 | * Provide a simple virtual geometry for legacy compatibility. For devices | |
1014 | * smaller than 1 MiB a small head and sector count is used to allow very | |
1015 | * tiny devices. For devices over 1 Mib a standard head and sector count | |
1016 | * is used to keep the cylinders count reasonable. | |
1017 | */ | |
1018 | static int | |
1019 | zvol_getgeo(struct block_device *bdev, struct hd_geometry *geo) | |
1020 | { | |
1021 | zvol_state_t *zv = bdev->bd_disk->private_data; | |
1022 | sector_t sectors = get_capacity(zv->zv_disk); | |
1023 | ||
1024 | if (sectors > 2048) { | |
1025 | geo->heads = 16; | |
1026 | geo->sectors = 63; | |
1027 | } else { | |
1028 | geo->heads = 2; | |
1029 | geo->sectors = 4; | |
1030 | } | |
1031 | ||
1032 | geo->start = 0; | |
1033 | geo->cylinders = sectors / (geo->heads * geo->sectors); | |
1034 | ||
1035 | return 0; | |
1036 | } | |
1037 | ||
1038 | static struct kobject * | |
1039 | zvol_probe(dev_t dev, int *part, void *arg) | |
1040 | { | |
1041 | zvol_state_t *zv; | |
1042 | struct kobject *kobj; | |
1043 | ||
1044 | mutex_enter(&zvol_state_lock); | |
1045 | zv = zvol_find_by_dev(dev); | |
1046 | kobj = zv ? get_disk(zv->zv_disk) : ERR_PTR(-ENOENT); | |
1047 | mutex_exit(&zvol_state_lock); | |
1048 | ||
1049 | return kobj; | |
1050 | } | |
1051 | ||
1052 | #ifdef HAVE_BDEV_BLOCK_DEVICE_OPERATIONS | |
1053 | static struct block_device_operations zvol_ops = { | |
1054 | .open = zvol_open, | |
1055 | .release = zvol_release, | |
1056 | .ioctl = zvol_ioctl, | |
1057 | .compat_ioctl = zvol_compat_ioctl, | |
1058 | .media_changed = zvol_media_changed, | |
1059 | .revalidate_disk = zvol_revalidate_disk, | |
1060 | .getgeo = zvol_getgeo, | |
1061 | .owner = THIS_MODULE, | |
1062 | }; | |
1063 | ||
1064 | #else /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */ | |
1065 | ||
1066 | static int | |
1067 | zvol_open_by_inode(struct inode *inode, struct file *file) | |
1068 | { | |
1069 | return zvol_open(inode->i_bdev, file->f_mode); | |
1070 | } | |
1071 | ||
1072 | static int | |
1073 | zvol_release_by_inode(struct inode *inode, struct file *file) | |
1074 | { | |
1075 | return zvol_release(inode->i_bdev->bd_disk, file->f_mode); | |
1076 | } | |
1077 | ||
1078 | static int | |
1079 | zvol_ioctl_by_inode(struct inode *inode, struct file *file, | |
1080 | unsigned int cmd, unsigned long arg) | |
1081 | { | |
b1c58213 NB |
1082 | if (file == NULL || inode == NULL) |
1083 | return -EINVAL; | |
60101509 BB |
1084 | return zvol_ioctl(inode->i_bdev, file->f_mode, cmd, arg); |
1085 | } | |
1086 | ||
1087 | # ifdef CONFIG_COMPAT | |
1088 | static long | |
1089 | zvol_compat_ioctl_by_inode(struct file *file, | |
1090 | unsigned int cmd, unsigned long arg) | |
1091 | { | |
b1c58213 NB |
1092 | if (file == NULL) |
1093 | return -EINVAL; | |
60101509 BB |
1094 | return zvol_compat_ioctl(file->f_dentry->d_inode->i_bdev, |
1095 | file->f_mode, cmd, arg); | |
1096 | } | |
1097 | # else | |
1098 | # define zvol_compat_ioctl_by_inode NULL | |
1099 | # endif | |
1100 | ||
1101 | static struct block_device_operations zvol_ops = { | |
1102 | .open = zvol_open_by_inode, | |
1103 | .release = zvol_release_by_inode, | |
1104 | .ioctl = zvol_ioctl_by_inode, | |
1105 | .compat_ioctl = zvol_compat_ioctl_by_inode, | |
1106 | .media_changed = zvol_media_changed, | |
1107 | .revalidate_disk = zvol_revalidate_disk, | |
1108 | .getgeo = zvol_getgeo, | |
1109 | .owner = THIS_MODULE, | |
1110 | }; | |
1111 | #endif /* HAVE_BDEV_BLOCK_DEVICE_OPERATIONS */ | |
1112 | ||
1113 | /* | |
1114 | * Allocate memory for a new zvol_state_t and setup the required | |
1115 | * request queue and generic disk structures for the block device. | |
1116 | */ | |
1117 | static zvol_state_t * | |
1118 | zvol_alloc(dev_t dev, const char *name) | |
1119 | { | |
1120 | zvol_state_t *zv; | |
1121 | ||
1122 | zv = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP); | |
1123 | if (zv == NULL) | |
1124 | goto out; | |
1125 | ||
1126 | zv->zv_queue = blk_init_queue(zvol_request, &zv->zv_lock); | |
1127 | if (zv->zv_queue == NULL) | |
1128 | goto out_kmem; | |
1129 | ||
b18019d2 ED |
1130 | #ifdef HAVE_BLK_QUEUE_FLUSH |
1131 | blk_queue_flush(zv->zv_queue, VDEV_REQ_FLUSH | VDEV_REQ_FUA); | |
1132 | #else | |
1133 | blk_queue_ordered(zv->zv_queue, QUEUE_ORDERED_DRAIN, NULL); | |
1134 | #endif /* HAVE_BLK_QUEUE_FLUSH */ | |
1135 | ||
60101509 BB |
1136 | zv->zv_disk = alloc_disk(ZVOL_MINORS); |
1137 | if (zv->zv_disk == NULL) | |
1138 | goto out_queue; | |
1139 | ||
1140 | zv->zv_queue->queuedata = zv; | |
1141 | zv->zv_dev = dev; | |
1142 | zv->zv_open_count = 0; | |
4c0d8e50 | 1143 | strlcpy(zv->zv_name, name, MAXNAMELEN); |
60101509 BB |
1144 | |
1145 | mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL); | |
1146 | avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare, | |
1147 | sizeof (rl_t), offsetof(rl_t, r_node)); | |
3c4988c8 BB |
1148 | zv->zv_znode.z_is_zvol = TRUE; |
1149 | ||
60101509 BB |
1150 | spin_lock_init(&zv->zv_lock); |
1151 | list_link_init(&zv->zv_next); | |
1152 | ||
1153 | zv->zv_disk->major = zvol_major; | |
1154 | zv->zv_disk->first_minor = (dev & MINORMASK); | |
1155 | zv->zv_disk->fops = &zvol_ops; | |
1156 | zv->zv_disk->private_data = zv; | |
1157 | zv->zv_disk->queue = zv->zv_queue; | |
4c0d8e50 FN |
1158 | snprintf(zv->zv_disk->disk_name, DISK_NAME_LEN, "%s%d", |
1159 | ZVOL_DEV_NAME, (dev & MINORMASK)); | |
60101509 BB |
1160 | |
1161 | return zv; | |
1162 | ||
1163 | out_queue: | |
1164 | blk_cleanup_queue(zv->zv_queue); | |
1165 | out_kmem: | |
1166 | kmem_free(zv, sizeof (zvol_state_t)); | |
1167 | out: | |
1168 | return NULL; | |
1169 | } | |
1170 | ||
1171 | /* | |
1172 | * Cleanup then free a zvol_state_t which was created by zvol_alloc(). | |
1173 | */ | |
1174 | static void | |
1175 | zvol_free(zvol_state_t *zv) | |
1176 | { | |
1177 | avl_destroy(&zv->zv_znode.z_range_avl); | |
1178 | mutex_destroy(&zv->zv_znode.z_range_lock); | |
1179 | ||
1180 | del_gendisk(zv->zv_disk); | |
1181 | blk_cleanup_queue(zv->zv_queue); | |
1182 | put_disk(zv->zv_disk); | |
1183 | ||
1184 | kmem_free(zv, sizeof (zvol_state_t)); | |
1185 | } | |
1186 | ||
1187 | static int | |
1188 | __zvol_create_minor(const char *name) | |
1189 | { | |
1190 | zvol_state_t *zv; | |
1191 | objset_t *os; | |
1192 | dmu_object_info_t *doi; | |
1193 | uint64_t volsize; | |
1194 | unsigned minor = 0; | |
1195 | int error = 0; | |
1196 | ||
1197 | ASSERT(MUTEX_HELD(&zvol_state_lock)); | |
1198 | ||
1199 | zv = zvol_find_by_name(name); | |
1200 | if (zv) { | |
1201 | error = EEXIST; | |
1202 | goto out; | |
1203 | } | |
1204 | ||
1205 | doi = kmem_alloc(sizeof(dmu_object_info_t), KM_SLEEP); | |
1206 | ||
1207 | error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, zvol_tag, &os); | |
1208 | if (error) | |
1209 | goto out_doi; | |
1210 | ||
1211 | error = dmu_object_info(os, ZVOL_OBJ, doi); | |
1212 | if (error) | |
1213 | goto out_dmu_objset_disown; | |
1214 | ||
1215 | error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize); | |
1216 | if (error) | |
1217 | goto out_dmu_objset_disown; | |
1218 | ||
1219 | error = zvol_find_minor(&minor); | |
1220 | if (error) | |
1221 | goto out_dmu_objset_disown; | |
1222 | ||
1223 | zv = zvol_alloc(MKDEV(zvol_major, minor), name); | |
1224 | if (zv == NULL) { | |
1225 | error = EAGAIN; | |
1226 | goto out_dmu_objset_disown; | |
1227 | } | |
1228 | ||
1229 | if (dmu_objset_is_snapshot(os)) | |
1230 | zv->zv_flags |= ZVOL_RDONLY; | |
1231 | ||
1232 | zv->zv_volblocksize = doi->doi_data_block_size; | |
1233 | zv->zv_volsize = volsize; | |
1234 | zv->zv_objset = os; | |
1235 | ||
1236 | set_capacity(zv->zv_disk, zv->zv_volsize >> 9); | |
1237 | ||
34037afe ED |
1238 | blk_queue_max_hw_sectors(zv->zv_queue, UINT_MAX); |
1239 | blk_queue_max_segments(zv->zv_queue, UINT16_MAX); | |
1240 | blk_queue_max_segment_size(zv->zv_queue, UINT_MAX); | |
1241 | blk_queue_physical_block_size(zv->zv_queue, zv->zv_volblocksize); | |
1242 | blk_queue_io_opt(zv->zv_queue, zv->zv_volblocksize); | |
30930fba ED |
1243 | #ifdef HAVE_BLK_QUEUE_DISCARD |
1244 | blk_queue_max_discard_sectors(zv->zv_queue, UINT_MAX); | |
1245 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, zv->zv_queue); | |
1246 | #endif | |
34037afe ED |
1247 | #ifdef HAVE_BLK_QUEUE_NONROT |
1248 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zv->zv_queue); | |
1249 | #endif | |
1250 | ||
60101509 BB |
1251 | if (zil_replay_disable) |
1252 | zil_destroy(dmu_objset_zil(os), B_FALSE); | |
1253 | else | |
1254 | zil_replay(os, zv, zvol_replay_vector); | |
1255 | ||
1256 | out_dmu_objset_disown: | |
1257 | dmu_objset_disown(os, zvol_tag); | |
1258 | zv->zv_objset = NULL; | |
1259 | out_doi: | |
1260 | kmem_free(doi, sizeof(dmu_object_info_t)); | |
1261 | out: | |
1262 | ||
1263 | if (error == 0) { | |
1264 | zvol_insert(zv); | |
1265 | add_disk(zv->zv_disk); | |
1266 | } | |
1267 | ||
1268 | return (error); | |
1269 | } | |
1270 | ||
1271 | /* | |
1272 | * Create a block device minor node and setup the linkage between it | |
1273 | * and the specified volume. Once this function returns the block | |
1274 | * device is live and ready for use. | |
1275 | */ | |
1276 | int | |
1277 | zvol_create_minor(const char *name) | |
1278 | { | |
1279 | int error; | |
1280 | ||
1281 | mutex_enter(&zvol_state_lock); | |
1282 | error = __zvol_create_minor(name); | |
1283 | mutex_exit(&zvol_state_lock); | |
1284 | ||
1285 | return (error); | |
1286 | } | |
1287 | ||
1288 | static int | |
1289 | __zvol_remove_minor(const char *name) | |
1290 | { | |
1291 | zvol_state_t *zv; | |
1292 | ||
1293 | ASSERT(MUTEX_HELD(&zvol_state_lock)); | |
1294 | ||
1295 | zv = zvol_find_by_name(name); | |
1296 | if (zv == NULL) | |
1297 | return (ENXIO); | |
1298 | ||
1299 | if (zv->zv_open_count > 0) | |
1300 | return (EBUSY); | |
1301 | ||
1302 | zvol_remove(zv); | |
1303 | zvol_free(zv); | |
1304 | ||
1305 | return (0); | |
1306 | } | |
1307 | ||
1308 | /* | |
1309 | * Remove a block device minor node for the specified volume. | |
1310 | */ | |
1311 | int | |
1312 | zvol_remove_minor(const char *name) | |
1313 | { | |
1314 | int error; | |
1315 | ||
1316 | mutex_enter(&zvol_state_lock); | |
1317 | error = __zvol_remove_minor(name); | |
1318 | mutex_exit(&zvol_state_lock); | |
1319 | ||
1320 | return (error); | |
1321 | } | |
1322 | ||
1323 | static int | |
1324 | zvol_create_minors_cb(spa_t *spa, uint64_t dsobj, | |
1325 | const char *dsname, void *arg) | |
1326 | { | |
1327 | if (strchr(dsname, '/') == NULL) | |
1328 | return 0; | |
1329 | ||
d5674448 BB |
1330 | (void) __zvol_create_minor(dsname); |
1331 | return (0); | |
60101509 BB |
1332 | } |
1333 | ||
1334 | /* | |
1335 | * Create minors for specified pool, if pool is NULL create minors | |
1336 | * for all available pools. | |
1337 | */ | |
1338 | int | |
1339 | zvol_create_minors(const char *pool) | |
1340 | { | |
1341 | spa_t *spa = NULL; | |
1342 | int error = 0; | |
1343 | ||
1344 | mutex_enter(&zvol_state_lock); | |
1345 | if (pool) { | |
1346 | error = dmu_objset_find_spa(NULL, pool, zvol_create_minors_cb, | |
1347 | NULL, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS); | |
1348 | } else { | |
1349 | mutex_enter(&spa_namespace_lock); | |
1350 | while ((spa = spa_next(spa)) != NULL) { | |
1351 | error = dmu_objset_find_spa(NULL, | |
1352 | spa_name(spa), zvol_create_minors_cb, NULL, | |
1353 | DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS); | |
1354 | if (error) | |
1355 | break; | |
1356 | } | |
1357 | mutex_exit(&spa_namespace_lock); | |
1358 | } | |
1359 | mutex_exit(&zvol_state_lock); | |
1360 | ||
1361 | return error; | |
1362 | } | |
1363 | ||
1364 | /* | |
1365 | * Remove minors for specified pool, if pool is NULL remove all minors. | |
1366 | */ | |
1367 | void | |
1368 | zvol_remove_minors(const char *pool) | |
1369 | { | |
1370 | zvol_state_t *zv, *zv_next; | |
1371 | char *str; | |
1372 | ||
4c0d8e50 | 1373 | str = kmem_zalloc(MAXNAMELEN, KM_SLEEP); |
60101509 BB |
1374 | if (pool) { |
1375 | (void) strncpy(str, pool, strlen(pool)); | |
1376 | (void) strcat(str, "/"); | |
1377 | } | |
1378 | ||
1379 | mutex_enter(&zvol_state_lock); | |
1380 | for (zv = list_head(&zvol_state_list); zv != NULL; zv = zv_next) { | |
1381 | zv_next = list_next(&zvol_state_list, zv); | |
1382 | ||
1383 | if (pool == NULL || !strncmp(str, zv->zv_name, strlen(str))) { | |
1384 | zvol_remove(zv); | |
1385 | zvol_free(zv); | |
1386 | } | |
1387 | } | |
1388 | mutex_exit(&zvol_state_lock); | |
4c0d8e50 | 1389 | kmem_free(str, MAXNAMELEN); |
60101509 BB |
1390 | } |
1391 | ||
1392 | int | |
1393 | zvol_init(void) | |
1394 | { | |
1395 | int error; | |
1396 | ||
60101509 | 1397 | zvol_taskq = taskq_create(ZVOL_DRIVER, zvol_threads, maxclsyspri, |
71011408 | 1398 | zvol_threads, INT_MAX, TASKQ_PREPOPULATE); |
60101509 BB |
1399 | if (zvol_taskq == NULL) { |
1400 | printk(KERN_INFO "ZFS: taskq_create() failed\n"); | |
1401 | return (-ENOMEM); | |
1402 | } | |
1403 | ||
1404 | error = register_blkdev(zvol_major, ZVOL_DRIVER); | |
1405 | if (error) { | |
1406 | printk(KERN_INFO "ZFS: register_blkdev() failed %d\n", error); | |
1407 | taskq_destroy(zvol_taskq); | |
1408 | return (error); | |
1409 | } | |
1410 | ||
1411 | blk_register_region(MKDEV(zvol_major, 0), 1UL << MINORBITS, | |
1412 | THIS_MODULE, zvol_probe, NULL, NULL); | |
1413 | ||
1414 | mutex_init(&zvol_state_lock, NULL, MUTEX_DEFAULT, NULL); | |
1415 | list_create(&zvol_state_list, sizeof (zvol_state_t), | |
1416 | offsetof(zvol_state_t, zv_next)); | |
1417 | ||
1418 | (void) zvol_create_minors(NULL); | |
1419 | ||
1420 | return (0); | |
1421 | } | |
1422 | ||
1423 | void | |
1424 | zvol_fini(void) | |
1425 | { | |
1426 | zvol_remove_minors(NULL); | |
1427 | blk_unregister_region(MKDEV(zvol_major, 0), 1UL << MINORBITS); | |
1428 | unregister_blkdev(zvol_major, ZVOL_DRIVER); | |
1429 | taskq_destroy(zvol_taskq); | |
1430 | mutex_destroy(&zvol_state_lock); | |
1431 | list_destroy(&zvol_state_list); | |
1432 | } | |
1433 | ||
30a9524e | 1434 | module_param(zvol_major, uint, 0444); |
60101509 BB |
1435 | MODULE_PARM_DESC(zvol_major, "Major number for zvol device"); |
1436 | ||
30a9524e | 1437 | module_param(zvol_threads, uint, 0444); |
60101509 | 1438 | MODULE_PARM_DESC(zvol_threads, "Number of threads for zvol device"); |