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>
| 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: stable/11/sys/boot/userboot/userboot/userboot_disk.c 315221 2017-03-14 02:06:03Z pfg $");
| 28__FBSDID("$FreeBSD: stable/11/sys/boot/userboot/userboot/userboot_disk.c 328889 2018-02-05 17:01:18Z kevans $");
|
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 42struct 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 49int userboot_disk_maxunit = 0; 50 51static int userdisk_maxunit = 0; 52static struct userdisk_info *ud_info; 53 54static int userdisk_init(void); 55static void userdisk_cleanup(void); 56static int userdisk_strategy(void *devdata, int flag, daddr_t dblk, 57 size_t size, char *buf, size_t *rsize); 58static int userdisk_realstrategy(void *devdata, int flag, daddr_t dblk, 59 size_t size, char *buf, size_t *rsize); 60static int userdisk_open(struct open_file *f, ...); 61static int userdisk_close(struct open_file *f); 62static int userdisk_ioctl(struct open_file *f, u_long cmd, void *data);
| 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 42struct 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 49int userboot_disk_maxunit = 0; 50 51static int userdisk_maxunit = 0; 52static struct userdisk_info *ud_info; 53 54static int userdisk_init(void); 55static void userdisk_cleanup(void); 56static int userdisk_strategy(void *devdata, int flag, daddr_t dblk, 57 size_t size, char *buf, size_t *rsize); 58static int userdisk_realstrategy(void *devdata, int flag, daddr_t dblk, 59 size_t size, char *buf, size_t *rsize); 60static int userdisk_open(struct open_file *f, ...); 61static int userdisk_close(struct open_file *f); 62static int userdisk_ioctl(struct open_file *f, u_long cmd, void *data);
|
63static void userdisk_print(int verbose);
| 63static int userdisk_print(int verbose);
|
64 65struct 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 */ 80static int 81userdisk_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) != NULL || CALLBACK(diskioctl, i, 95 DIOCGMEDIASIZE, &mediasize) != NULL) 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 107static void 108userdisk_cleanup(void) 109{ 110 111 if (userdisk_maxunit > 0) 112 free(ud_info); 113 disk_cleanup(&userboot_disk); 114} 115 116/* 117 * Print information about disks 118 */
| 64 65struct 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 */ 80static int 81userdisk_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) != NULL || CALLBACK(diskioctl, i, 95 DIOCGMEDIASIZE, &mediasize) != NULL) 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 107static void 108userdisk_cleanup(void) 109{ 110 111 if (userdisk_maxunit > 0) 112 free(ud_info); 113 disk_cleanup(&userboot_disk); 114} 115 116/* 117 * Print information about disks 118 */
|
119static void
| 119static int
|
120userdisk_print(int verbose) 121{ 122 struct disk_devdesc dev; 123 char line[80];
| 120userdisk_print(int verbose) 121{ 122 struct disk_devdesc dev; 123 char line[80];
|
124 int i;
| 124 int i, ret = 0;
|
125
| 125
|
| 126 if (userdisk_maxunit == 0) 127 return (0); 128 129 printf("%s devices:", userboot_disk.dv_name); 130 if ((ret = pager_output("\n")) != 0) 131 return (ret); 132
|
126 for (i = 0; i < userdisk_maxunit; i++) {
| 133 for (i = 0; i < userdisk_maxunit; i++) {
|
127 sprintf(line, " disk%d: Guest drive image\n", i); 128 pager_output(line);
| 134 snprintf(line, sizeof(line), 135 " disk%d: Guest drive image\n", i); 136 ret = pager_output(line); 137 if (ret != 0) 138 break;
|
129 dev.d_dev = &userboot_disk; 130 dev.d_unit = i; 131 dev.d_slice = -1; 132 dev.d_partition = -1; 133 if (disk_open(&dev, ud_info[i].mediasize, 134 ud_info[i].sectorsize, 0) == 0) {
| 139 dev.d_dev = &userboot_disk; 140 dev.d_unit = i; 141 dev.d_slice = -1; 142 dev.d_partition = -1; 143 if (disk_open(&dev, ud_info[i].mediasize, 144 ud_info[i].sectorsize, 0) == 0) {
|
135 sprintf(line, " disk%d", i); 136 disk_print(&dev, line, verbose);
| 145 snprintf(line, sizeof(line), " disk%d", i); 146 ret = disk_print(&dev, line, verbose);
|
137 disk_close(&dev);
| 147 disk_close(&dev);
|
| 148 if (ret != 0) 149 break;
|
138 } 139 }
| 150 } 151 }
|
| 152 return (ret);
|
140} 141 142/* 143 * Attempt to open the disk described by (dev) for use by (f). 144 */ 145static int 146userdisk_open(struct open_file *f, ...) 147{ 148 va_list ap; 149 struct disk_devdesc *dev; 150 151 va_start(ap, f); 152 dev = va_arg(ap, struct disk_devdesc *); 153 va_end(ap); 154 155 if (dev->d_unit < 0 || dev->d_unit >= userdisk_maxunit) 156 return (EIO); 157 ud_info[dev->d_unit].ud_open++; 158 if (ud_info[dev->d_unit].ud_bcache == NULL) 159 ud_info[dev->d_unit].ud_bcache = bcache_allocate(); 160 return (disk_open(dev, ud_info[dev->d_unit].mediasize, 161 ud_info[dev->d_unit].sectorsize, 0)); 162} 163 164static int 165userdisk_close(struct open_file *f) 166{ 167 struct disk_devdesc *dev; 168 169 dev = (struct disk_devdesc *)f->f_devdata; 170 ud_info[dev->d_unit].ud_open--; 171 if (ud_info[dev->d_unit].ud_open == 0) { 172 bcache_free(ud_info[dev->d_unit].ud_bcache); 173 ud_info[dev->d_unit].ud_bcache = NULL; 174 } 175 return (disk_close(dev)); 176} 177 178static int 179userdisk_strategy(void *devdata, int rw, daddr_t dblk, size_t size, 180 char *buf, size_t *rsize) 181{ 182 struct bcache_devdata bcd; 183 struct disk_devdesc *dev; 184 185 dev = (struct disk_devdesc *)devdata; 186 bcd.dv_strategy = userdisk_realstrategy; 187 bcd.dv_devdata = devdata; 188 bcd.dv_cache = ud_info[dev->d_unit].ud_bcache; 189 return (bcache_strategy(&bcd, rw, dblk + dev->d_offset, 190 size, buf, rsize)); 191} 192 193static int 194userdisk_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size, 195 char *buf, size_t *rsize) 196{ 197 struct disk_devdesc *dev = devdata; 198 uint64_t off; 199 size_t resid; 200 int rc; 201 202 if (rw == F_WRITE) 203 return (EROFS); 204 if (rw != F_READ) 205 return (EINVAL); 206 if (rsize) 207 *rsize = 0; 208 off = dblk * ud_info[dev->d_unit].sectorsize; 209 rc = CALLBACK(diskread, dev->d_unit, off, buf, size, &resid); 210 if (rc) 211 return (rc); 212 if (rsize) 213 *rsize = size - resid; 214 return (0); 215} 216 217static int 218userdisk_ioctl(struct open_file *f, u_long cmd, void *data) 219{ 220 struct disk_devdesc *dev; 221 222 dev = (struct disk_devdesc *)f->f_devdata; 223 return (CALLBACK(diskioctl, dev->d_unit, cmd, data)); 224}
| 153} 154 155/* 156 * Attempt to open the disk described by (dev) for use by (f). 157 */ 158static int 159userdisk_open(struct open_file *f, ...) 160{ 161 va_list ap; 162 struct disk_devdesc *dev; 163 164 va_start(ap, f); 165 dev = va_arg(ap, struct disk_devdesc *); 166 va_end(ap); 167 168 if (dev->d_unit < 0 || dev->d_unit >= userdisk_maxunit) 169 return (EIO); 170 ud_info[dev->d_unit].ud_open++; 171 if (ud_info[dev->d_unit].ud_bcache == NULL) 172 ud_info[dev->d_unit].ud_bcache = bcache_allocate(); 173 return (disk_open(dev, ud_info[dev->d_unit].mediasize, 174 ud_info[dev->d_unit].sectorsize, 0)); 175} 176 177static int 178userdisk_close(struct open_file *f) 179{ 180 struct disk_devdesc *dev; 181 182 dev = (struct disk_devdesc *)f->f_devdata; 183 ud_info[dev->d_unit].ud_open--; 184 if (ud_info[dev->d_unit].ud_open == 0) { 185 bcache_free(ud_info[dev->d_unit].ud_bcache); 186 ud_info[dev->d_unit].ud_bcache = NULL; 187 } 188 return (disk_close(dev)); 189} 190 191static int 192userdisk_strategy(void *devdata, int rw, daddr_t dblk, size_t size, 193 char *buf, size_t *rsize) 194{ 195 struct bcache_devdata bcd; 196 struct disk_devdesc *dev; 197 198 dev = (struct disk_devdesc *)devdata; 199 bcd.dv_strategy = userdisk_realstrategy; 200 bcd.dv_devdata = devdata; 201 bcd.dv_cache = ud_info[dev->d_unit].ud_bcache; 202 return (bcache_strategy(&bcd, rw, dblk + dev->d_offset, 203 size, buf, rsize)); 204} 205 206static int 207userdisk_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size, 208 char *buf, size_t *rsize) 209{ 210 struct disk_devdesc *dev = devdata; 211 uint64_t off; 212 size_t resid; 213 int rc; 214 215 if (rw == F_WRITE) 216 return (EROFS); 217 if (rw != F_READ) 218 return (EINVAL); 219 if (rsize) 220 *rsize = 0; 221 off = dblk * ud_info[dev->d_unit].sectorsize; 222 rc = CALLBACK(diskread, dev->d_unit, off, buf, size, &resid); 223 if (rc) 224 return (rc); 225 if (rsize) 226 *rsize = size - resid; 227 return (0); 228} 229 230static int 231userdisk_ioctl(struct open_file *f, u_long cmd, void *data) 232{ 233 struct disk_devdesc *dev; 234 235 dev = (struct disk_devdesc *)f->f_devdata; 236 return (CALLBACK(diskioctl, dev->d_unit, cmd, data)); 237}
|