1 /*
2  * Copyright (C) 2010      Advanced Micro Devices
3  * Author Christoph Egger <Christoph.Egger@amd.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published
7  * by the Free Software Foundation; version 2.1 only.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Lesser General Public License for more details.
13  */
14 
15 #include "libxl_osdeps.h" /* must come before any other headers */
16 #include "libxl_internal.h"
17 
18 #include "tap-ctl.h"
19 
libxl__blktap_enabled(libxl__gc * gc)20 int libxl__blktap_enabled(libxl__gc *gc)
21 {
22     const char *msg;
23     return !tap_ctl_check(&msg);
24 }
25 
libxl__blktap_devpath(libxl__gc * gc,const char * disk,libxl_disk_format format)26 char *libxl__blktap_devpath(libxl__gc *gc,
27                             const char *disk,
28                             libxl_disk_format format)
29 {
30     const char *type;
31     char *params, *devname = NULL;
32     tap_list_t tap;
33     int err;
34 
35     type = libxl__device_disk_string_of_format(format);
36     err = tap_ctl_find(type, disk, &tap);
37     if (err == 0) {
38         devname = GCSPRINTF("/dev/xen/blktap-2/tapdev%d", tap.minor);
39         if (devname)
40             return devname;
41     }
42 
43     params = GCSPRINTF("%s:%s", type, disk);
44     err = tap_ctl_create(params, &devname);
45     if (!err) {
46         libxl__ptr_add(gc, devname);
47         return devname;
48     }
49 
50     free(devname);
51     return NULL;
52 }
53 
54 
libxl__device_destroy_tapdisk(libxl__gc * gc,const char * params)55 int libxl__device_destroy_tapdisk(libxl__gc *gc, const char *params)
56 {
57     char *type, *disk;
58     int err;
59     tap_list_t tap;
60 
61     type = libxl__strdup(gc, params);
62 
63     disk = strchr(type, ':');
64     if (!disk) {
65         LOG(ERROR, "Unable to parse params %s", params);
66         return ERROR_INVAL;
67     }
68 
69     *disk++ = '\0';
70 
71     err = tap_ctl_find(type, disk, &tap);
72     if (err < 0) {
73         /* returns -errno */
74         LOGEV(ERROR, -err, "Unable to find type %s disk %s", type, disk);
75         return ERROR_FAIL;
76     }
77 
78     err = tap_ctl_destroy(tap.id, tap.minor);
79     if (err < 0) {
80         LOGEV(ERROR, -err, "Failed to destroy tap device id %d minor %d",
81               tap.id, tap.minor);
82         return ERROR_FAIL;
83     }
84 
85     return 0;
86 }
87 
88 /*
89  * Local variables:
90  * mode: C
91  * c-basic-offset: 4
92  * indent-tabs-mode: nil
93  * End:
94  */
95