2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002 Juli Mallett. All rights reserved.
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
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.
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.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/mount.h>
36 #include <sys/disklabel.h>
39 #include <ufs/ufs/ufsmount.h>
40 #include <ufs/ufs/dinode.h>
41 #include <ufs/ffs/fs.h>
53 bread(struct uufsd *disk, ufs2_daddr_t blockno, void *data, size_t size)
62 * XXX: various disk controllers require alignment of our buffer
63 * XXX: which is stricter than struct alignment.
64 * XXX: Bounce the buffer if not 64 byte aligned.
65 * XXX: this can be removed if/when the kernel is fixed
67 if (((intptr_t)data) & 0x3f) {
70 ERROR(disk, "allocate bounce buffer");
74 cnt = pread(disk->d_fd, p2, size, (off_t)(blockno * disk->d_bsize));
76 ERROR(disk, "read error from block device");
80 ERROR(disk, "end of file from block device");
83 if ((size_t)cnt != size) {
84 ERROR(disk, "short read or read error from block device");
88 memcpy(data, p2, size);
92 fail: memset(data, 0, size);
100 bwrite(struct uufsd *disk, ufs2_daddr_t blockno, const void *data, size_t size)
108 rv = ufs_disk_write(disk);
110 ERROR(disk, "failed to open disk for writing");
115 * XXX: various disk controllers require alignment of our buffer
116 * XXX: which is stricter than struct alignment.
117 * XXX: Bounce the buffer if not 64 byte aligned.
118 * XXX: this can be removed if/when the kernel is fixed
120 if (((intptr_t)data) & 0x3f) {
123 ERROR(disk, "allocate bounce buffer");
126 memcpy(p2, data, size);
129 cnt = pwrite(disk->d_fd, data, size, (off_t)(blockno * disk->d_bsize));
133 ERROR(disk, "write error to block device");
136 if ((size_t)cnt != size) {
137 ERROR(disk, "short write to block device");
144 #ifdef __FreeBSD_kernel__
147 berase_helper(struct uufsd *disk, ufs2_daddr_t blockno, ufs2_daddr_t size)
151 ioarg[0] = blockno * disk->d_bsize;
153 return (ioctl(disk->d_fd, DIOCGDELETE, ioarg));
159 berase_helper(struct uufsd *disk, ufs2_daddr_t blockno, ufs2_daddr_t size)
162 off_t offset, zero_chunk_size, pwrite_size;
165 offset = blockno * disk->d_bsize;
166 zero_chunk_size = 65536 * disk->d_bsize;
167 zero_chunk = calloc(1, zero_chunk_size);
168 if (zero_chunk == NULL) {
169 ERROR(disk, "failed to allocate memory");
174 if (pwrite_size > zero_chunk_size)
175 pwrite_size = zero_chunk_size;
176 rv = pwrite(disk->d_fd, zero_chunk, pwrite_size, offset);
178 ERROR(disk, "failed writing to disk");
192 berase(struct uufsd *disk, ufs2_daddr_t blockno, ufs2_daddr_t size)
197 rv = ufs_disk_write(disk);
199 ERROR(disk, "failed to open disk for writing");
202 return (berase_helper(disk, blockno, size));