1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2002 Juli Mallett. All rights reserved. 5 * 6 * This software was written by Juli Mallett <jmallett@FreeBSD.org> for the 7 * FreeBSD project. Redistribution and use in source and binary forms, with 8 * or without modification, are permitted provided that the following 9 * conditions are met: 10 * 11 * 1. Redistribution of source code must retain the above copyright notice, 12 * this list of conditions and the following disclaimer. 13 * 2. Redistribution in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 26 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/param.h> 34 #include <sys/mount.h> 35 #include <sys/disk.h> 36 #include <sys/disklabel.h> 37 #include <sys/stat.h> 38 39 #include <ufs/ufs/extattr.h> 40 #include <ufs/ufs/quota.h> 41 #include <ufs/ufs/ufsmount.h> 42 #include <ufs/ufs/dinode.h> 43 #include <ufs/ffs/fs.h> 44 45 #include <errno.h> 46 #include <fcntl.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <unistd.h> 51 52 #include <libufs.h> 53 54 ssize_t 55 bread(struct uufsd *disk, ufs2_daddr_t blockno, void *data, size_t size) 56 { 57 void *p2; 58 ssize_t cnt; 59 60 ERROR(disk, NULL); 61 62 p2 = data; 63 /* 64 * XXX: various disk controllers require alignment of our buffer 65 * XXX: which is stricter than struct alignment. 66 * XXX: Bounce the buffer if not 64 byte aligned. 67 * XXX: this can be removed if/when the kernel is fixed 68 */ 69 if (((intptr_t)data) & 0x3f) { 70 p2 = malloc(size); 71 if (p2 == NULL) { 72 ERROR(disk, "allocate bounce buffer"); 73 goto fail; 74 } 75 } 76 cnt = pread(disk->d_fd, p2, size, (off_t)(blockno * disk->d_bsize)); 77 if (cnt == -1) { 78 ERROR(disk, "read error from block device"); 79 goto fail; 80 } 81 if (cnt == 0) { 82 ERROR(disk, "end of file from block device"); 83 goto fail; 84 } 85 if ((size_t)cnt != size) { 86 ERROR(disk, "short read or read error from block device"); 87 goto fail; 88 } 89 if (p2 != data) { 90 memcpy(data, p2, size); 91 free(p2); 92 } 93 return (cnt); 94 fail: memset(data, 0, size); 95 if (p2 != data) { 96 free(p2); 97 } 98 return (-1); 99 } 100 101 ssize_t 102 bwrite(struct uufsd *disk, ufs2_daddr_t blockno, const void *data, size_t size) 103 { 104 ssize_t cnt; 105 int rv; 106 void *p2 = NULL; 107 108 ERROR(disk, NULL); 109 110 rv = ufs_disk_write(disk); 111 if (rv == -1) { 112 ERROR(disk, "failed to open disk for writing"); 113 return (-1); 114 } 115 116 /* 117 * XXX: various disk controllers require alignment of our buffer 118 * XXX: which is stricter than struct alignment. 119 * XXX: Bounce the buffer if not 64 byte aligned. 120 * XXX: this can be removed if/when the kernel is fixed 121 */ 122 if (((intptr_t)data) & 0x3f) { 123 p2 = malloc(size); 124 if (p2 == NULL) { 125 ERROR(disk, "allocate bounce buffer"); 126 return (-1); 127 } 128 memcpy(p2, data, size); 129 data = p2; 130 } 131 cnt = pwrite(disk->d_fd, data, size, (off_t)(blockno * disk->d_bsize)); 132 if (p2 != NULL) 133 free(p2); 134 if (cnt == -1) { 135 ERROR(disk, "write error to block device"); 136 return (-1); 137 } 138 if ((size_t)cnt != size) { 139 ERROR(disk, "short write to block device"); 140 return (-1); 141 } 142 143 return (cnt); 144 } 145 146 #ifdef __FreeBSD_kernel__ 147 148 static int 149 berase_helper(struct uufsd *disk, ufs2_daddr_t blockno, ufs2_daddr_t size) 150 { 151 off_t ioarg[2]; 152 153 ioarg[0] = blockno * disk->d_bsize; 154 ioarg[1] = size; 155 return (ioctl(disk->d_fd, DIOCGDELETE, ioarg)); 156 } 157 158 #else 159 160 static int 161 berase_helper(struct uufsd *disk, ufs2_daddr_t blockno, ufs2_daddr_t size) 162 { 163 char *zero_chunk; 164 off_t offset, zero_chunk_size, pwrite_size; 165 int rv; 166 167 offset = blockno * disk->d_bsize; 168 zero_chunk_size = 65536 * disk->d_bsize; 169 zero_chunk = calloc(1, zero_chunk_size); 170 if (zero_chunk == NULL) { 171 ERROR(disk, "failed to allocate memory"); 172 return (-1); 173 } 174 while (size > 0) { 175 pwrite_size = size; 176 if (pwrite_size > zero_chunk_size) 177 pwrite_size = zero_chunk_size; 178 rv = pwrite(disk->d_fd, zero_chunk, pwrite_size, offset); 179 if (rv == -1) { 180 ERROR(disk, "failed writing to disk"); 181 break; 182 } 183 size -= rv; 184 offset += rv; 185 rv = 0; 186 } 187 free(zero_chunk); 188 return (rv); 189 } 190 191 #endif 192 193 int 194 berase(struct uufsd *disk, ufs2_daddr_t blockno, ufs2_daddr_t size) 195 { 196 int rv; 197 198 ERROR(disk, NULL); 199 rv = ufs_disk_write(disk); 200 if (rv == -1) { 201 ERROR(disk, "failed to open disk for writing"); 202 return(rv); 203 } 204 return (berase_helper(disk, blockno, size)); 205 } 206