1 /*
2  * Copyright (c) 2008, XenSource Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of XenSource Inc. nor the names of its contributors
13  *       may be used to endorse or promote products derived from this software
14  *       without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
20  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 #ifndef __TAP_CTL_H__
29 #define __TAP_CTL_H__
30 
31 #include <syslog.h>
32 #include <errno.h>
33 #include <tapdisk-message.h>
34 
35 extern int tap_ctl_debug;
36 
37 #ifdef TAPCTL
38 #define DBG(_f, _a...)				\
39 	do {					\
40 		if (tap_ctl_debug)		\
41 			printf(_f, ##_a);	\
42 	} while (0)
43 
44 #define DPRINTF(_f, _a...) syslog(LOG_INFO, _f, ##_a)
45 #define EPRINTF(_f, _a...) syslog(LOG_ERR, "tap-err:%s: " _f, __func__, ##_a)
46 #define  PERROR(_f, _a...) syslog(LOG_ERR, "tap-err:%s: " _f ": %s", __func__, ##_a, \
47 				  strerror(errno))
48 #endif
49 
50 void tap_ctl_version(int *major, int *minor);
51 int tap_ctl_kernel_version(int *major, int *minor);
52 
53 int tap_ctl_check_blktap(const char **message);
54 int tap_ctl_check_version(const char **message);
55 int tap_ctl_check(const char **message);
56 
57 int tap_ctl_connect(const char *path, int *socket);
58 int tap_ctl_connect_id(int id, int *socket);
59 int tap_ctl_read_message(int fd, tapdisk_message_t *message, int timeout);
60 int tap_ctl_write_message(int fd, tapdisk_message_t *message, int timeout);
61 int tap_ctl_send_and_receive(int fd, tapdisk_message_t *message, int timeout);
62 int tap_ctl_connect_send_and_receive(int id,
63 				     tapdisk_message_t *message, int timeout);
64 char *tap_ctl_socket_name(int id);
65 
66 typedef struct {
67 	int         id;
68 	pid_t       pid;
69 	int         minor;
70 	int         state;
71 	char       *type;
72 	char       *path;
73 } tap_list_t;
74 
75 int tap_ctl_get_driver_id(const char *handle);
76 
77 int tap_ctl_list(tap_list_t ***list);
78 void tap_ctl_free_list(tap_list_t **list);
79 int tap_ctl_find(const char *type, const char *path, tap_list_t *tap);
80 
81 int tap_ctl_allocate(int *minor, char **devname);
82 int tap_ctl_free(const int minor);
83 
84 int tap_ctl_create(const char *params, char **devname);
85 int tap_ctl_destroy(const int id, const int minor);
86 
87 int tap_ctl_spawn(void);
88 pid_t tap_ctl_get_pid(const int id);
89 
90 int tap_ctl_attach(const int id, const int minor);
91 int tap_ctl_detach(const int id, const int minor);
92 
93 int tap_ctl_open(const int id, const int minor, const char *params);
94 int tap_ctl_close(const int id, const int minor, const int force);
95 
96 int tap_ctl_pause(const int id, const int minor);
97 int tap_ctl_unpause(const int id, const int minor, const char *params);
98 
99 int tap_ctl_blk_major(void);
100 
101 #endif
102