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 #include <sys/param.h> 32 #include <sys/mount.h> 33 #include <sys/disk.h> 34 #include <sys/disklabel.h> 35 #include <sys/stat.h> 36 37 #include <ufs/ufs/extattr.h> 38 #include <ufs/ufs/quota.h> 39 #include <ufs/ufs/ufsmount.h> 40 #include <ufs/ufs/dinode.h> 41 #include <ufs/ffs/fs.h> 42 43 #include <errno.h> 44 #include <fcntl.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <string.h> 48 #include <unistd.h> 49 50 #include <libufs.h> 51 52 ssize_t 53 bread(struct uufsd *disk, ufs2_daddr_t blockno, void *data, size_t size) 54 { 55 void *p2; 56 ssize_t cnt; 57 58 ERROR(disk, NULL); 59 60 BUF_MALLOC(&p2, data, size); 61 if (p2 == NULL) { 62 ERROR(disk, "allocate bounce buffer"); 63 goto fail; 64 } 65 cnt = pread(disk->d_fd, p2, size, (off_t)(blockno * disk->d_bsize)); 66 if (cnt == -1) { 67 ERROR(disk, "read error from block device"); 68 goto fail; 69 } 70 if (cnt == 0) { 71 ERROR(disk, "end of file from block device"); 72 goto fail; 73 } 74 if ((size_t)cnt != size) { 75 ERROR(disk, "short read or read error from block device"); 76 goto fail; 77 } 78 if (p2 != data) { 79 memcpy(data, p2, size); 80 free(p2); 81 } 82 return (cnt); 83 fail: memset(data, 0, size); 84 if (p2 != data) { 85 free(p2); 86 } 87 return (-1); 88 } 89 90 ssize_t 91 bwrite(struct uufsd *disk, ufs2_daddr_t blockno, const void *data, size_t size) 92 { 93 ssize_t cnt; 94 int rv; 95 void *p2; 96 97 ERROR(disk, NULL); 98 99 rv = ufs_disk_write(disk); 100 if (rv == -1) { 101 ERROR(disk, "failed to open disk for writing"); 102 return (-1); 103 } 104 BUF_MALLOC(&p2, data, size); 105 if (p2 == NULL) { 106 ERROR(disk, "allocate bounce buffer"); 107 return (-1); 108 } 109 if (p2 != data) 110 memcpy(p2, data, size); 111 cnt = pwrite(disk->d_fd, p2, size, (off_t)(blockno * disk->d_bsize)); 112 if (p2 != data) 113 free(p2); 114 if (cnt == -1) { 115 ERROR(disk, "write error to block device"); 116 return (-1); 117 } 118 if ((size_t)cnt != size) { 119 ERROR(disk, "short write to block device"); 120 return (-1); 121 } 122 return (cnt); 123 } 124 125 #ifdef __FreeBSD_kernel__ 126 127 static int 128 berase_helper(struct uufsd *disk, ufs2_daddr_t blockno, ufs2_daddr_t size) 129 { 130 off_t ioarg[2]; 131 132 ioarg[0] = blockno * disk->d_bsize; 133 ioarg[1] = size; 134 return (ioctl(disk->d_fd, DIOCGDELETE, ioarg)); 135 } 136 137 #else 138 139 static int 140 berase_helper(struct uufsd *disk, ufs2_daddr_t blockno, ufs2_daddr_t size) 141 { 142 char *zero_chunk; 143 off_t offset, zero_chunk_size, pwrite_size; 144 int rv; 145 146 offset = blockno * disk->d_bsize; 147 zero_chunk_size = 65536 * disk->d_bsize; 148 zero_chunk = calloc(1, zero_chunk_size); 149 if (zero_chunk == NULL) { 150 ERROR(disk, "failed to allocate memory"); 151 return (-1); 152 } 153 while (size > 0) { 154 pwrite_size = size; 155 if (pwrite_size > zero_chunk_size) 156 pwrite_size = zero_chunk_size; 157 rv = pwrite(disk->d_fd, zero_chunk, pwrite_size, offset); 158 if (rv == -1) { 159 ERROR(disk, "failed writing to disk"); 160 break; 161 } 162 size -= rv; 163 offset += rv; 164 rv = 0; 165 } 166 free(zero_chunk); 167 return (rv); 168 } 169 170 #endif 171 172 int 173 berase(struct uufsd *disk, ufs2_daddr_t blockno, ufs2_daddr_t size) 174 { 175 int rv; 176 177 ERROR(disk, NULL); 178 rv = ufs_disk_write(disk); 179 if (rv == -1) { 180 ERROR(disk, "failed to open disk for writing"); 181 return(rv); 182 } 183 return (berase_helper(disk, blockno, size)); 184 } 185