2 * Copyright (c) 2012 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/sysctl.h>
41 #include <sys/disklabel.h>
42 #include <sys/disklabel32.h>
43 #include <sys/disklabel64.h>
44 #include <sys/diskslice.h>
45 #include <sys/diskmbr.h>
47 #include <sys/malloc.h>
48 #include <sys/device.h>
49 #include <sys/devfs.h>
50 #include <sys/thread.h>
51 #include <sys/queue.h>
58 #include <sys/mplock2.h>
59 #include <sys/msgport2.h>
60 #include <sys/thread2.h>
73 static MALLOC_DEFINE(M_DMSG_DISK
, "dmsg_disk", "disk dmsg");
75 static int blk_active
;
76 SYSCTL_INT(_debug
, OID_AUTO
, blk_active
, CTLFLAG_RW
, &blk_active
, 0,
77 "Number of active iocom IOs");
79 static int disk_iocom_reconnect(struct disk
*dp
, struct file
*fp
);
80 static int disk_rcvdmsg(kdmsg_msg_t
*msg
);
82 static void disk_blk_open(struct disk
*dp
, kdmsg_msg_t
*msg
);
83 static void disk_blk_read(struct disk
*dp
, kdmsg_msg_t
*msg
);
84 static void disk_blk_write(struct disk
*dp
, kdmsg_msg_t
*msg
);
85 static void disk_blk_flush(struct disk
*dp
, kdmsg_msg_t
*msg
);
86 static void disk_blk_freeblks(struct disk
*dp
, kdmsg_msg_t
*msg
);
87 static void diskiodone(struct bio
*bio
);
90 disk_iocom_init(struct disk
*dp
)
92 kdmsg_iocom_init(&dp
->d_iocom
, dp
,
93 KDMSG_IOCOMF_AUTOCONN
|
94 KDMSG_IOCOMF_AUTORXSPAN
|
95 KDMSG_IOCOMF_AUTOTXSPAN
,
96 M_DMSG_DISK
, disk_rcvdmsg
);
100 disk_iocom_update(struct disk
*dp
)
105 disk_iocom_uninit(struct disk
*dp
)
107 kdmsg_iocom_uninit(&dp
->d_iocom
);
111 disk_iocom_ioctl(struct disk
*dp
, int cmd
, void *data
)
114 struct disk_ioc_recluster
*recl
;
120 fp
= holdfp(curproc
->p_fd
, recl
->fd
, -1);
122 error
= disk_iocom_reconnect(dp
, fp
);
136 disk_iocom_reconnect(struct disk
*dp
, struct file
*fp
)
140 ksnprintf(devname
, sizeof(devname
), "%s%d",
141 dev_dname(dp
->d_rawdev
), dkunit(dp
->d_rawdev
));
143 kdmsg_iocom_reconnect(&dp
->d_iocom
, fp
, devname
);
145 dp
->d_iocom
.auto_lnk_conn
.proto_version
= DMSG_SPAN_PROTO_1
;
146 dp
->d_iocom
.auto_lnk_conn
.peer_type
= DMSG_PEER_BLOCK
;
147 dp
->d_iocom
.auto_lnk_conn
.peer_mask
= 1LLU << DMSG_PEER_BLOCK
;
148 dp
->d_iocom
.auto_lnk_conn
.peer_mask
= (uint64_t)-1;
150 if (dp
->d_info
.d_serialno
) {
151 ksnprintf(dp
->d_iocom
.auto_lnk_conn
.peer_label
,
152 sizeof(dp
->d_iocom
.auto_lnk_conn
.peer_label
),
153 "%s/%s", hostname
, dp
->d_info
.d_serialno
);
155 ksnprintf(dp
->d_iocom
.auto_lnk_conn
.peer_label
,
156 sizeof(dp
->d_iocom
.auto_lnk_conn
.peer_label
),
157 "%s/%s", hostname
, devname
);
160 ksnprintf(dp
->d_iocom
.auto_lnk_conn
.peer_label
,
161 sizeof(dp
->d_iocom
.auto_lnk_conn
.peer_label
),
162 "%s/%s", hostname
, devname
);
164 dp
->d_iocom
.auto_lnk_span
.proto_version
= DMSG_SPAN_PROTO_1
;
165 dp
->d_iocom
.auto_lnk_span
.peer_type
= DMSG_PEER_BLOCK
;
166 dp
->d_iocom
.auto_lnk_span
.media
.block
.bytes
=
167 dp
->d_info
.d_media_size
;
168 dp
->d_iocom
.auto_lnk_span
.media
.block
.blksize
=
169 dp
->d_info
.d_media_blksize
;
170 ksnprintf(dp
->d_iocom
.auto_lnk_span
.peer_label
,
171 sizeof(dp
->d_iocom
.auto_lnk_span
.peer_label
),
172 "%s", dp
->d_iocom
.auto_lnk_conn
.peer_label
);
173 if (dp
->d_info
.d_serialno
) {
174 ksnprintf(dp
->d_iocom
.auto_lnk_span
.pfs_label
,
175 sizeof(dp
->d_iocom
.auto_lnk_span
.pfs_label
),
176 "%s", dp
->d_info
.d_serialno
);
179 * If no serial number is available generate a dummy serial
180 * number from the host and device name and pray. This will
181 * allow e.g. /dev/vn* to look meaningful on a remote machine.
183 ksnprintf(dp
->d_iocom
.auto_lnk_span
.pfs_label
,
184 sizeof(dp
->d_iocom
.auto_lnk_span
.pfs_label
),
185 "%s.%s", hostname
, devname
);
188 kdmsg_iocom_autoinitiate(&dp
->d_iocom
, NULL
);
194 disk_rcvdmsg(kdmsg_msg_t
*msg
)
196 struct disk
*dp
= msg
->state
->iocom
->handle
;
199 * Handle debug messages (these might not be in transactions)
201 switch(msg
->any
.head
.cmd
& DMSGF_CMDSWMASK
) {
204 * Execute shell command (not supported atm)
206 kdmsg_msg_reply(msg
, DMSG_ERR_NOSUPP
);
208 case DMSG_DBG_SHELL
| DMSGF_REPLY
:
210 msg
->aux_data
[msg
->aux_size
- 1] = 0;
211 kprintf("diskiocom: DEBUGMSG: %s\n", msg
->aux_data
);
217 * All remaining messages must be in a transaction.
219 * NOTE! We currently don't care if the transaction is just
220 * the span transaction (for disk probes) or if it is the
221 * BLK_OPEN transaction.
223 * NOTE! We are switching on the first message's command. The
224 * actual message command within the transaction may be
225 * different (if streaming within a transaction).
227 if (msg
->state
== &msg
->state
->iocom
->state0
) {
228 kdmsg_msg_reply(msg
, DMSG_ERR_NOSUPP
);
232 switch(msg
->state
->rxcmd
& DMSGF_CMDSWMASK
) {
234 disk_blk_open(dp
, msg
);
238 * not reached normally but leave in for completeness
240 disk_blk_read(dp
, msg
);
243 disk_blk_write(dp
, msg
);
246 disk_blk_flush(dp
, msg
);
248 case DMSG_BLK_FREEBLKS
:
249 disk_blk_freeblks(dp
, msg
);
252 if ((msg
->any
.head
.cmd
& DMSGF_REPLY
) == 0) {
253 if (msg
->any
.head
.cmd
& DMSGF_DELETE
)
254 kdmsg_msg_reply(msg
, DMSG_ERR_NOSUPP
);
256 kdmsg_msg_result(msg
, DMSG_ERR_NOSUPP
);
265 disk_blk_open(struct disk
*dp
, kdmsg_msg_t
*msg
)
267 struct dios_open
*openst
;
268 int error
= DMSG_ERR_NOSUPP
;
271 openst
= msg
->state
->any
.any
;
272 if ((msg
->any
.head
.cmd
& DMSGF_CMDSWMASK
) == DMSG_BLK_OPEN
) {
273 if (openst
== NULL
) {
274 openst
= kmalloc(sizeof(*openst
), M_DEVBUF
,
276 msg
->state
->any
.any
= openst
;
279 if (msg
->any
.blk_open
.modes
& DMSG_BLKOPEN_RD
)
281 if (msg
->any
.blk_open
.modes
& DMSG_BLKOPEN_WR
)
283 error
= dev_dopen(dp
->d_rawdev
, fflags
, S_IFCHR
, proc0
.p_ucred
, NULL
);
287 if (msg
->any
.blk_open
.modes
& DMSG_BLKOPEN_RD
)
289 if (msg
->any
.blk_open
.modes
& DMSG_BLKOPEN_WR
)
294 if ((msg
->any
.head
.cmd
& DMSGF_CMDSWMASK
) == DMSG_BLK_CLOSE
&&
297 if ((msg
->any
.blk_open
.modes
& DMSG_BLKOPEN_RD
) &&
301 if ((msg
->any
.blk_open
.modes
& DMSG_BLKOPEN_WR
) &&
305 error
= dev_dclose(dp
->d_rawdev
, fflags
, S_IFCHR
, NULL
);
309 if (msg
->any
.blk_open
.modes
& DMSG_BLKOPEN_RD
)
311 if (msg
->any
.blk_open
.modes
& DMSG_BLKOPEN_WR
)
316 if (msg
->any
.head
.cmd
& DMSGF_DELETE
) {
318 while (openst
->openrd
&& openst
->openwr
) {
321 dev_dclose(dp
->d_rawdev
, FREAD
|FWRITE
, S_IFCHR
, NULL
);
323 while (openst
->openrd
) {
325 dev_dclose(dp
->d_rawdev
, FREAD
, S_IFCHR
, NULL
);
327 while (openst
->openwr
) {
329 dev_dclose(dp
->d_rawdev
, FWRITE
, S_IFCHR
, NULL
);
331 kfree(openst
, M_DEVBUF
);
332 msg
->state
->any
.any
= NULL
;
334 kdmsg_msg_reply(msg
, error
);
336 kdmsg_msg_result(msg
, error
);
342 disk_blk_read(struct disk
*dp
, kdmsg_msg_t
*msg
)
344 struct dios_io
*iost
;
347 int error
= DMSG_ERR_NOSUPP
;
351 * Only DMSG_BLK_READ commands imply read ops.
353 iost
= msg
->state
->any
.any
;
354 if ((msg
->any
.head
.cmd
& DMSGF_CMDSWMASK
) == DMSG_BLK_READ
) {
355 if (msg
->any
.blk_read
.bytes
< DEV_BSIZE
||
356 msg
->any
.blk_read
.bytes
> MAXPHYS
) {
357 error
= DMSG_ERR_PARAM
;
361 iost
= kmalloc(sizeof(*iost
), M_DEVBUF
,
363 msg
->state
->any
.any
= iost
;
366 bp
= geteblk(msg
->any
.blk_read
.bytes
);
368 bp
->b_cmd
= BUF_CMD_READ
;
369 bp
->b_bcount
= msg
->any
.blk_read
.bytes
;
370 bp
->b_resid
= bp
->b_bcount
;
371 bio
->bio_offset
= msg
->any
.blk_read
.offset
;
372 bio
->bio_caller_info1
.ptr
= msg
->state
;
373 bio
->bio_done
= diskiodone
;
375 /* kdmsg_state_hold(msg->state); */
376 atomic_add_int(&blk_active
, 1);
377 atomic_add_int(&iost
->count
, 1);
378 if (msg
->any
.head
.cmd
& DMSGF_DELETE
)
381 dev_dstrategy(dp
->d_rawdev
, bio
);
385 if (msg
->any
.head
.cmd
& DMSGF_DELETE
) {
386 if (iost
&& iost
->count
== 0) {
387 kfree(iost
, M_DEVBUF
);
388 msg
->state
->any
.any
= NULL
;
390 kdmsg_msg_reply(msg
, error
);
392 kdmsg_msg_result(msg
, error
);
399 disk_blk_write(struct disk
*dp
, kdmsg_msg_t
*msg
)
401 struct dios_io
*iost
;
404 int error
= DMSG_ERR_NOSUPP
;
408 * Only DMSG_BLK_WRITE commands imply read ops.
410 iost
= msg
->state
->any
.any
;
411 if ((msg
->any
.head
.cmd
& DMSGF_CMDSWMASK
) == DMSG_BLK_WRITE
) {
412 if (msg
->any
.blk_write
.bytes
< DEV_BSIZE
||
413 msg
->any
.blk_write
.bytes
> MAXPHYS
) {
414 error
= DMSG_ERR_PARAM
;
418 iost
= kmalloc(sizeof(*iost
), M_DEVBUF
,
420 msg
->state
->any
.any
= iost
;
424 * Issue WRITE. Short data implies zeros. Try to optimize
425 * the buffer cache buffer for the case where we can just
426 * use the message's data pointer.
429 if (msg
->aux_size
>= msg
->any
.blk_write
.bytes
)
432 bp
= geteblk(msg
->any
.blk_write
.bytes
);
434 bp
->b_cmd
= BUF_CMD_WRITE
;
435 bp
->b_bcount
= msg
->any
.blk_write
.bytes
;
436 bp
->b_resid
= bp
->b_bcount
;
437 if (msg
->aux_size
>= msg
->any
.blk_write
.bytes
) {
438 bp
->b_data
= msg
->aux_data
;
439 kdmsg_detach_aux_data(msg
, &iost
->data
);
441 bcopy(msg
->aux_data
, bp
->b_data
, msg
->aux_size
);
442 bzero(bp
->b_data
+ msg
->aux_size
,
443 msg
->any
.blk_write
.bytes
- msg
->aux_size
);
444 bzero(&iost
->data
, sizeof(iost
->data
));
446 bio
->bio_offset
= msg
->any
.blk_write
.offset
;
447 bio
->bio_caller_info1
.ptr
= msg
->state
;
448 bio
->bio_done
= diskiodone
;
450 /* kdmsg_state_hold(msg->state); */
451 atomic_add_int(&blk_active
, 1);
452 atomic_add_int(&iost
->count
, 1);
453 if (msg
->any
.head
.cmd
& DMSGF_DELETE
)
456 dev_dstrategy(dp
->d_rawdev
, bio
);
460 if (msg
->any
.head
.cmd
& DMSGF_DELETE
) {
461 if (iost
&& iost
->count
== 0) {
462 kfree(iost
, M_DEVBUF
);
463 msg
->state
->any
.any
= NULL
;
465 kdmsg_msg_reply(msg
, error
);
467 kdmsg_msg_result(msg
, error
);
474 disk_blk_flush(struct disk
*dp
, kdmsg_msg_t
*msg
)
476 struct dios_io
*iost
;
479 int error
= DMSG_ERR_NOSUPP
;
483 * Only DMSG_BLK_FLUSH commands imply read ops.
485 iost
= msg
->state
->any
.any
;
486 if ((msg
->any
.head
.cmd
& DMSGF_CMDSWMASK
) == DMSG_BLK_FLUSH
) {
488 iost
= kmalloc(sizeof(*iost
), M_DEVBUF
,
490 msg
->state
->any
.any
= iost
;
495 bp
->b_cmd
= BUF_CMD_FLUSH
;
496 bp
->b_bcount
= msg
->any
.blk_flush
.bytes
;
498 bio
->bio_offset
= msg
->any
.blk_flush
.offset
;
499 bio
->bio_caller_info1
.ptr
= msg
->state
;
500 bio
->bio_done
= diskiodone
;
502 /* kdmsg_state_hold(msg->state); */
503 atomic_add_int(&blk_active
, 1);
504 atomic_add_int(&iost
->count
, 1);
505 if (msg
->any
.head
.cmd
& DMSGF_DELETE
)
508 dev_dstrategy(dp
->d_rawdev
, bio
);
511 if (msg
->any
.head
.cmd
& DMSGF_DELETE
) {
512 if (iost
&& iost
->count
== 0) {
513 kfree(iost
, M_DEVBUF
);
514 msg
->state
->any
.any
= NULL
;
516 kdmsg_msg_reply(msg
, error
);
518 kdmsg_msg_result(msg
, error
);
525 disk_blk_freeblks(struct disk
*dp
, kdmsg_msg_t
*msg
)
527 struct dios_io
*iost
;
530 int error
= DMSG_ERR_NOSUPP
;
534 * Only DMSG_BLK_FREEBLKS commands imply read ops.
536 iost
= msg
->state
->any
.any
;
537 if ((msg
->any
.head
.cmd
& DMSGF_CMDSWMASK
) == DMSG_BLK_FREEBLKS
) {
539 iost
= kmalloc(sizeof(*iost
), M_DEVBUF
,
541 msg
->state
->any
.any
= iost
;
546 bp
->b_cmd
= BUF_CMD_FREEBLKS
;
547 bp
->b_bcount
= msg
->any
.blk_freeblks
.bytes
;
549 bio
->bio_offset
= msg
->any
.blk_freeblks
.offset
;
550 bio
->bio_caller_info1
.ptr
= msg
->state
;
551 bio
->bio_done
= diskiodone
;
553 /* kdmsg_state_hold(msg->state); */
554 atomic_add_int(&blk_active
, 1);
555 atomic_add_int(&iost
->count
, 1);
556 if (msg
->any
.head
.cmd
& DMSGF_DELETE
)
559 dev_dstrategy(dp
->d_rawdev
, bio
);
562 if (msg
->any
.head
.cmd
& DMSGF_DELETE
) {
563 if (iost
&& iost
->count
== 0) {
564 kfree(iost
, M_DEVBUF
);
565 msg
->state
->any
.any
= NULL
;
567 kdmsg_msg_reply(msg
, error
);
569 kdmsg_msg_result(msg
, error
);
576 diskiodone(struct bio
*bio
)
578 struct buf
*bp
= bio
->bio_buf
;
579 kdmsg_state_t
*state
= bio
->bio_caller_info1
.ptr
;
581 struct dios_io
*iost
= state
->any
.any
;
588 cmd
= DMSG_LNK_ERROR
;
594 cmd
= DMSG_LNK_ERROR
;
596 bytes
= bp
->b_bcount
;
599 if (bp
->b_flags
& B_ERROR
) {
605 kdmsg_free_aux_data(&iost
->data
);
608 case BUF_CMD_FREEBLKS
:
609 if (bp
->b_flags
& B_ERROR
)
615 panic("diskiodone: Unknown bio cmd = %d\n",
616 bio
->bio_buf
->b_cmd
);
617 error
= 0; /* avoid compiler warning */
618 break; /* NOT REACHED */
622 * Convert error to DMSG_ERR_* code.
628 * Convert LNK_ERROR or BLK_ERROR if non-zero resid. READS will
629 * have already converted cmd to BLK_ERROR and set up data to return.
631 if (resid
&& cmd
== DMSG_LNK_ERROR
)
632 cmd
= DMSG_BLK_ERROR
;
633 /* XXX txcmd is delayed so this won't work for streaming */
634 if ((state
->txcmd
& DMSGF_CREATE
) == 0) /* assume serialized */
637 if (atomic_fetchadd_int(&iost
->count
, -1) == 1)
640 atomic_add_int(&iost
->count
, -1);
642 atomic_add_int(&blk_active
, -1);
646 * Allocate a basic or extended reply. Be careful not to populate
647 * extended header fields unless we allocated an extended reply.
649 rmsg
= kdmsg_msg_alloc(state
, cmd
, NULL
, 0);
651 rmsg
->aux_data
= kmalloc(bytes
, state
->iocom
->mmsg
, M_INTWAIT
);
652 rmsg
->aux_size
= bytes
;
653 rmsg
->flags
|= KDMSG_FLAG_AUXALLOC
;
654 bcopy(data
, rmsg
->aux_data
, bytes
);
656 rmsg
->any
.blk_error
.head
.error
= error
;
657 if ((cmd
& DMSGF_BASECMDMASK
) == DMSG_BLK_ERROR
)
658 rmsg
->any
.blk_error
.resid
= resid
;
659 bio
->bio_caller_info1
.ptr
= NULL
;
660 /* kdmsg_state_drop(state); */
661 kdmsg_msg_write(rmsg
);
662 if (bp
->b_flags
& B_PAGING
) {
663 relpbuf(bio
->bio_buf
, NULL
);
665 bp
->b_flags
|= B_INVAL
| B_AGE
;