1 /*- 2 * Copyright (c) 2011 Google, 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 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 /* 31 * Userboot disk image handling. 32 */ 33 34 #include <sys/disk.h> 35 #include <stand.h> 36 #include <stdarg.h> 37 #include <bootstrap.h> 38 39 #include "disk.h" 40 #include "libuserboot.h" 41 42 struct userdisk_info { 43 uint64_t mediasize; 44 uint16_t sectorsize; 45 int ud_open; /* reference counter */ 46 void *ud_bcache; /* buffer cache data */ 47 }; 48 49 int userboot_disk_maxunit = 0; 50 51 static int userdisk_maxunit = 0; 52 static struct userdisk_info *ud_info; 53 54 static int userdisk_init(void); 55 static void userdisk_cleanup(void); 56 static int userdisk_strategy(void *devdata, int flag, daddr_t dblk, 57 size_t size, char *buf, size_t *rsize); 58 static int userdisk_realstrategy(void *devdata, int flag, daddr_t dblk, 59 size_t size, char *buf, size_t *rsize); 60 static int userdisk_open(struct open_file *f, ...); 61 static int userdisk_close(struct open_file *f); 62 static int userdisk_ioctl(struct open_file *f, u_long cmd, void *data); 63 static int userdisk_print(int verbose); 64 65 struct devsw userboot_disk = { 66 "disk", 67 DEVT_DISK, 68 userdisk_init, 69 userdisk_strategy, 70 userdisk_open, 71 userdisk_close, 72 userdisk_ioctl, 73 userdisk_print, 74 userdisk_cleanup 75 }; 76 77 /* 78 * Initialize userdisk_info structure for each disk. 79 */ 80 static int 81 userdisk_init(void) 82 { 83 off_t mediasize; 84 u_int sectorsize; 85 int i; 86 87 userdisk_maxunit = userboot_disk_maxunit; 88 if (userdisk_maxunit > 0) { 89 ud_info = malloc(sizeof(*ud_info) * userdisk_maxunit); 90 if (ud_info == NULL) 91 return (ENOMEM); 92 for (i = 0; i < userdisk_maxunit; i++) { 93 if (CALLBACK(diskioctl, i, DIOCGSECTORSIZE, 94 §orsize) != 0 || CALLBACK(diskioctl, i, 95 DIOCGMEDIASIZE, &mediasize) != 0) 96 return (ENXIO); 97 ud_info[i].mediasize = mediasize; 98 ud_info[i].sectorsize = sectorsize; 99 ud_info[i].ud_open = 0; 100 ud_info[i].ud_bcache = NULL; 101 } 102 } 103 bcache_add_dev(userdisk_maxunit); 104 return(0); 105 } 106 107 static void 108 userdisk_cleanup(void) 109 { 110 111 if (userdisk_maxunit > 0) 112 free(ud_info); 113 } 114 115 /* 116 * Print information about disks 117 */ 118 static int 119 userdisk_print(int verbose) 120 { 121 struct disk_devdesc dev; 122 char line[80]; 123 int i, ret = 0; 124 125 if (userdisk_maxunit == 0) 126 return (0); 127 128 printf("%s devices:", userboot_disk.dv_name); 129 if ((ret = pager_output("\n")) != 0) 130 return (ret); 131 132 for (i = 0; i < userdisk_maxunit; i++) { 133 snprintf(line, sizeof(line), 134 " disk%d: Guest drive image\n", i); 135 ret = pager_output(line); 136 if (ret != 0) 137 break; 138 dev.dd.d_dev = &userboot_disk; 139 dev.dd.d_unit = i; 140 dev.d_slice = D_SLICENONE; 141 dev.d_partition = D_PARTNONE; 142 if (disk_open(&dev, ud_info[i].mediasize, 143 ud_info[i].sectorsize) == 0) { 144 snprintf(line, sizeof(line), " disk%d", i); 145 ret = disk_print(&dev, line, verbose); 146 disk_close(&dev); 147 if (ret != 0) 148 break; 149 } 150 } 151 return (ret); 152 } 153 154 /* 155 * Attempt to open the disk described by (dev) for use by (f). 156 */ 157 static int 158 userdisk_open(struct open_file *f, ...) 159 { 160 va_list ap; 161 struct disk_devdesc *dev; 162 163 va_start(ap, f); 164 dev = va_arg(ap, struct disk_devdesc *); 165 va_end(ap); 166 167 if (dev->dd.d_unit < 0 || dev->dd.d_unit >= userdisk_maxunit) 168 return (EIO); 169 ud_info[dev->dd.d_unit].ud_open++; 170 if (ud_info[dev->dd.d_unit].ud_bcache == NULL) 171 ud_info[dev->dd.d_unit].ud_bcache = bcache_allocate(); 172 return (disk_open(dev, ud_info[dev->dd.d_unit].mediasize, 173 ud_info[dev->dd.d_unit].sectorsize)); 174 } 175 176 static int 177 userdisk_close(struct open_file *f) 178 { 179 struct disk_devdesc *dev; 180 181 dev = (struct disk_devdesc *)f->f_devdata; 182 ud_info[dev->dd.d_unit].ud_open--; 183 if (ud_info[dev->dd.d_unit].ud_open == 0) { 184 bcache_free(ud_info[dev->dd.d_unit].ud_bcache); 185 ud_info[dev->dd.d_unit].ud_bcache = NULL; 186 } 187 return (disk_close(dev)); 188 } 189 190 static int 191 userdisk_strategy(void *devdata, int rw, daddr_t dblk, size_t size, 192 char *buf, size_t *rsize) 193 { 194 struct bcache_devdata bcd; 195 struct disk_devdesc *dev; 196 197 dev = (struct disk_devdesc *)devdata; 198 bcd.dv_strategy = userdisk_realstrategy; 199 bcd.dv_devdata = devdata; 200 bcd.dv_cache = ud_info[dev->dd.d_unit].ud_bcache; 201 return (bcache_strategy(&bcd, rw, dblk + dev->d_offset, 202 size, buf, rsize)); 203 } 204 205 static int 206 userdisk_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size, 207 char *buf, size_t *rsize) 208 { 209 struct disk_devdesc *dev = devdata; 210 uint64_t off; 211 size_t resid; 212 int rc; 213 214 if (rsize) 215 *rsize = 0; 216 off = dblk * ud_info[dev->dd.d_unit].sectorsize; 217 switch (rw & F_MASK) { 218 case F_READ: 219 rc = CALLBACK(diskread, dev->dd.d_unit, off, buf, size, &resid); 220 break; 221 case F_WRITE: 222 rc = CALLBACK(diskwrite, dev->dd.d_unit, off, buf, size, 223 &resid); 224 break; 225 default: 226 rc = EINVAL; 227 break; 228 } 229 if (rc) 230 return (rc); 231 if (rsize) 232 *rsize = size - resid; 233 return (0); 234 } 235 236 static int 237 userdisk_ioctl(struct open_file *f, u_long cmd, void *data) 238 { 239 struct disk_devdesc *dev; 240 int rc; 241 242 dev = (struct disk_devdesc *)f->f_devdata; 243 rc = disk_ioctl(dev, cmd, data); 244 if (rc != ENOTTY) 245 return (rc); 246 247 return (CALLBACK(diskioctl, dev->dd.d_unit, cmd, data)); 248 } 249