769486626ae1259b6b2aed23c01c75b9a49f1d6a
[dragonfly.git] / sys / kern / subr_diskiocom.c
1 /*
2  * Copyright (c) 2012 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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
16  *    distribution.
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.
20  *
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
32  * SUCH DAMAGE.
33  */
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/proc.h>
38 #include <sys/sysctl.h>
39 #include <sys/buf.h>
40 #include <sys/conf.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>
46 #include <sys/disk.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>
52 #include <sys/lock.h>
53 #include <sys/uuid.h>
54
55 #include <sys/dmsg.h>
56
57 #include <sys/buf2.h>
58 #include <sys/mplock2.h>
59 #include <sys/msgport2.h>
60 #include <sys/thread2.h>
61
62 static MALLOC_DEFINE(M_DMSG_DISK, "dmsg_disk", "disk dmsg");
63
64 static int disk_iocom_reconnect(struct disk *dp, struct file *fp);
65 static int disk_rcvdmsg(kdmsg_msg_t *msg);
66
67 void
68 disk_iocom_init(struct disk *dp)
69 {
70         kdmsg_iocom_init(&dp->d_iocom, dp,
71                          KDMSG_IOCOMF_AUTOCONN |
72                          KDMSG_IOCOMF_AUTOSPAN |
73                          KDMSG_IOCOMF_AUTOCIRC,
74                          M_DMSG_DISK, disk_rcvdmsg);
75 }
76
77 void
78 disk_iocom_update(struct disk *dp)
79 {
80 }
81
82 void
83 disk_iocom_uninit(struct disk *dp)
84 {
85         kdmsg_iocom_uninit(&dp->d_iocom);
86 }
87
88 int
89 disk_iocom_ioctl(struct disk *dp, int cmd, void *data)
90 {
91         struct file *fp;
92         struct disk_ioc_recluster *recl;
93         int error;
94
95         switch(cmd) {
96         case DIOCRECLUSTER:
97                 recl = data;
98                 fp = holdfp(curproc->p_fd, recl->fd, -1);
99                 if (fp) {
100                         error = disk_iocom_reconnect(dp, fp);
101                 } else {
102                         error = EINVAL;
103                 }
104                 break;
105         default:
106                 error = EOPNOTSUPP;
107                 break;
108         }
109         return error;
110 }
111
112 static
113 int
114 disk_iocom_reconnect(struct disk *dp, struct file *fp)
115 {
116         char devname[64];
117
118         ksnprintf(devname, sizeof(devname), "%s%d",
119                   dev_dname(dp->d_rawdev), dkunit(dp->d_rawdev));
120
121         kdmsg_iocom_reconnect(&dp->d_iocom, fp, devname);
122
123         dp->d_iocom.auto_lnk_conn.pfs_type = DMSG_PFSTYPE_SERVER;
124         dp->d_iocom.auto_lnk_conn.proto_version = DMSG_SPAN_PROTO_1;
125         dp->d_iocom.auto_lnk_conn.peer_type = DMSG_PEER_BLOCK;
126         dp->d_iocom.auto_lnk_conn.peer_mask = 1LLU << DMSG_PEER_BLOCK;
127         dp->d_iocom.auto_lnk_conn.pfs_mask = (uint64_t)-1;
128         ksnprintf(dp->d_iocom.auto_lnk_conn.cl_label,
129                   sizeof(dp->d_iocom.auto_lnk_conn.cl_label),
130                   "%s/%s", hostname, devname);
131         if (dp->d_info.d_serialno) {
132                 ksnprintf(dp->d_iocom.auto_lnk_conn.fs_label,
133                           sizeof(dp->d_iocom.auto_lnk_conn.fs_label),
134                           "%s", dp->d_info.d_serialno);
135         }
136
137         dp->d_iocom.auto_lnk_span.pfs_type = DMSG_PFSTYPE_SERVER;
138         dp->d_iocom.auto_lnk_span.proto_version = DMSG_SPAN_PROTO_1;
139         dp->d_iocom.auto_lnk_span.peer_type = DMSG_PEER_BLOCK;
140         dp->d_iocom.auto_lnk_span.media.block.bytes =
141                                                 dp->d_info.d_media_size;
142         dp->d_iocom.auto_lnk_span.media.block.blksize =
143                                                 dp->d_info.d_media_blksize;
144         ksnprintf(dp->d_iocom.auto_lnk_span.cl_label,
145                   sizeof(dp->d_iocom.auto_lnk_span.cl_label),
146                   "%s/%s", hostname, devname);
147         if (dp->d_info.d_serialno) {
148                 ksnprintf(dp->d_iocom.auto_lnk_span.fs_label,
149                           sizeof(dp->d_iocom.auto_lnk_span.fs_label),
150                           "%s", dp->d_info.d_serialno);
151         }
152
153         kdmsg_iocom_autoinitiate(&dp->d_iocom, NULL);
154
155         return (0);
156 }
157
158 int
159 disk_rcvdmsg(kdmsg_msg_t *msg)
160 {
161         struct disk *dp = msg->iocom->handle;
162
163         switch(msg->any.head.cmd & DMSGF_TRANSMASK) {
164         case DMSG_DBG_SHELL:
165                 /*
166                  * Execute shell command (not supported atm)
167                  */
168                 kdmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
169                 break;
170         case DMSG_DBG_SHELL | DMSGF_REPLY:
171                 if (msg->aux_data) {
172                         msg->aux_data[msg->aux_size - 1] = 0;
173                         kprintf("diskiocom: DEBUGMSG: %s\n", msg->aux_data);
174                 }
175                 break;
176         case DMSG_BLK_OPEN | DMSGF_CREATE:
177         case DMSG_BLK_READ | DMSGF_CREATE:
178         case DMSG_BLK_WRITE | DMSGF_CREATE:
179         case DMSG_BLK_FLUSH | DMSGF_CREATE:
180         case DMSG_BLK_FREEBLKS | DMSGF_CREATE:
181         default:
182                 kprintf("diskiocom: DISK ADHOC INPUT %s%d cmd %08x\n",
183                         dev_dname(dp->d_rawdev), dkunit(dp->d_rawdev),
184                         msg->any.head.cmd);
185                 if (msg->any.head.cmd & DMSGF_CREATE)
186                         kdmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
187                 break;
188         }
189         return (0);
190 }