xref: /freebsd/lib/libufs/block.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
15e53a4f9SPedro F. Giffuni /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro F. Giffuni  *
420938dbfSJuli Mallett  * Copyright (c) 2002 Juli Mallett.  All rights reserved.
520938dbfSJuli Mallett  *
620938dbfSJuli Mallett  * This software was written by Juli Mallett <jmallett@FreeBSD.org> for the
720938dbfSJuli Mallett  * FreeBSD project.  Redistribution and use in source and binary forms, with
820938dbfSJuli Mallett  * or without modification, are permitted provided that the following
920938dbfSJuli Mallett  * conditions are met:
1020938dbfSJuli Mallett  *
1120938dbfSJuli Mallett  * 1. Redistribution of source code must retain the above copyright notice,
1220938dbfSJuli Mallett  *    this list of conditions and the following disclaimer.
1320938dbfSJuli Mallett  * 2. Redistribution in binary form must reproduce the above copyright
1420938dbfSJuli Mallett  *    notice, this list of conditions and the following disclaimer in the
1520938dbfSJuli Mallett  *    documentation and/or other materials provided with the distribution.
1620938dbfSJuli Mallett  *
1720938dbfSJuli Mallett  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1820938dbfSJuli Mallett  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1920938dbfSJuli Mallett  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2020938dbfSJuli Mallett  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
2120938dbfSJuli Mallett  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2220938dbfSJuli Mallett  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2320938dbfSJuli Mallett  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2420938dbfSJuli Mallett  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
2520938dbfSJuli Mallett  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
2620938dbfSJuli Mallett  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2720938dbfSJuli Mallett  * POSSIBILITY OF SUCH DAMAGE.
2820938dbfSJuli Mallett  */
2920938dbfSJuli Mallett 
3020938dbfSJuli Mallett #include <sys/param.h>
3120938dbfSJuli Mallett #include <sys/mount.h>
3220a0f65bSPoul-Henning Kamp #include <sys/disk.h>
3320938dbfSJuli Mallett #include <sys/disklabel.h>
3420938dbfSJuli Mallett #include <sys/stat.h>
3520938dbfSJuli Mallett 
36d485c77fSKonstantin Belousov #include <ufs/ufs/extattr.h>
37d485c77fSKonstantin Belousov #include <ufs/ufs/quota.h>
3820938dbfSJuli Mallett #include <ufs/ufs/ufsmount.h>
3920938dbfSJuli Mallett #include <ufs/ufs/dinode.h>
4020938dbfSJuli Mallett #include <ufs/ffs/fs.h>
4120938dbfSJuli Mallett 
4220938dbfSJuli Mallett #include <errno.h>
433ff1f264SJuli Mallett #include <fcntl.h>
4420938dbfSJuli Mallett #include <stdio.h>
45ed1eac71SPoul-Henning Kamp #include <stdlib.h>
4620938dbfSJuli Mallett #include <string.h>
4720938dbfSJuli Mallett #include <unistd.h>
4820938dbfSJuli Mallett 
4920938dbfSJuli Mallett #include <libufs.h>
5020938dbfSJuli Mallett 
5120938dbfSJuli Mallett ssize_t
bread(struct uufsd * disk,ufs2_daddr_t blockno,void * data,size_t size)5220938dbfSJuli Mallett bread(struct uufsd *disk, ufs2_daddr_t blockno, void *data, size_t size)
5320938dbfSJuli Mallett {
54ed1eac71SPoul-Henning Kamp 	void *p2;
5520938dbfSJuli Mallett 	ssize_t cnt;
5620938dbfSJuli Mallett 
5749b2a686SJuli Mallett 	ERROR(disk, NULL);
5820938dbfSJuli Mallett 
59*772430ddSKirk McKusick 	BUF_MALLOC(&p2, data, size);
6006f53575SXin LI 	if (p2 == NULL) {
61ed1eac71SPoul-Henning Kamp 		ERROR(disk, "allocate bounce buffer");
6206f53575SXin LI 		goto fail;
6306f53575SXin LI 	}
64ed1eac71SPoul-Henning Kamp 	cnt = pread(disk->d_fd, p2, size, (off_t)(blockno * disk->d_bsize));
65a506dcdaSJuli Mallett 	if (cnt == -1) {
66a506dcdaSJuli Mallett 		ERROR(disk, "read error from block device");
67a506dcdaSJuli Mallett 		goto fail;
68a506dcdaSJuli Mallett 	}
69a506dcdaSJuli Mallett 	if (cnt == 0) {
70a506dcdaSJuli Mallett 		ERROR(disk, "end of file from block device");
71a506dcdaSJuli Mallett 		goto fail;
72a506dcdaSJuli Mallett 	}
73a506dcdaSJuli Mallett 	if ((size_t)cnt != size) {
74a506dcdaSJuli Mallett 		ERROR(disk, "short read or read error from block device");
75a506dcdaSJuli Mallett 		goto fail;
7620938dbfSJuli Mallett 	}
77ed1eac71SPoul-Henning Kamp 	if (p2 != data) {
78ed1eac71SPoul-Henning Kamp 		memcpy(data, p2, size);
79ed1eac71SPoul-Henning Kamp 		free(p2);
80ed1eac71SPoul-Henning Kamp 	}
811081253fSJuli Mallett 	return (cnt);
82ed1eac71SPoul-Henning Kamp fail:	memset(data, 0, size);
83ed1eac71SPoul-Henning Kamp 	if (p2 != data) {
84ed1eac71SPoul-Henning Kamp 		free(p2);
85ed1eac71SPoul-Henning Kamp 	}
861081253fSJuli Mallett 	return (-1);
8720938dbfSJuli Mallett }
8820938dbfSJuli Mallett 
8920938dbfSJuli Mallett ssize_t
bwrite(struct uufsd * disk,ufs2_daddr_t blockno,const void * data,size_t size)9020938dbfSJuli Mallett bwrite(struct uufsd *disk, ufs2_daddr_t blockno, const void *data, size_t size)
9120938dbfSJuli Mallett {
9220938dbfSJuli Mallett 	ssize_t cnt;
937dc95357SJuli Mallett 	int rv;
94*772430ddSKirk McKusick 	void *p2;
9520938dbfSJuli Mallett 
9649b2a686SJuli Mallett 	ERROR(disk, NULL);
9720938dbfSJuli Mallett 
987dc95357SJuli Mallett 	rv = ufs_disk_write(disk);
997dc95357SJuli Mallett 	if (rv == -1) {
1003ff1f264SJuli Mallett 		ERROR(disk, "failed to open disk for writing");
1011081253fSJuli Mallett 		return (-1);
1023ff1f264SJuli Mallett 	}
103*772430ddSKirk McKusick 	BUF_MALLOC(&p2, data, size);
10406f53575SXin LI 	if (p2 == NULL) {
105ed1eac71SPoul-Henning Kamp 		ERROR(disk, "allocate bounce buffer");
10606f53575SXin LI 		return (-1);
10706f53575SXin LI 	}
108*772430ddSKirk McKusick 	if (p2 != data)
109ed1eac71SPoul-Henning Kamp 		memcpy(p2, data, size);
110*772430ddSKirk McKusick 	cnt = pwrite(disk->d_fd, p2, size, (off_t)(blockno * disk->d_bsize));
111*772430ddSKirk McKusick 	if (p2 != data)
112ed1eac71SPoul-Henning Kamp 		free(p2);
113a506dcdaSJuli Mallett 	if (cnt == -1) {
1145b3a32e4SJuli Mallett 		ERROR(disk, "write error to block device");
1151081253fSJuli Mallett 		return (-1);
116a506dcdaSJuli Mallett 	}
117a506dcdaSJuli Mallett 	if ((size_t)cnt != size) {
11849b2a686SJuli Mallett 		ERROR(disk, "short write to block device");
1191081253fSJuli Mallett 		return (-1);
12020938dbfSJuli Mallett 	}
1211081253fSJuli Mallett 	return (cnt);
12220938dbfSJuli Mallett }
12320a0f65bSPoul-Henning Kamp 
124bcedb9c0SRobert Millan #ifdef __FreeBSD_kernel__
125bcedb9c0SRobert Millan 
126bcedb9c0SRobert Millan static int
berase_helper(struct uufsd * disk,ufs2_daddr_t blockno,ufs2_daddr_t size)127bcedb9c0SRobert Millan berase_helper(struct uufsd *disk, ufs2_daddr_t blockno, ufs2_daddr_t size)
128bcedb9c0SRobert Millan {
129bcedb9c0SRobert Millan 	off_t ioarg[2];
130bcedb9c0SRobert Millan 
131bcedb9c0SRobert Millan 	ioarg[0] = blockno * disk->d_bsize;
132bcedb9c0SRobert Millan 	ioarg[1] = size;
133bcedb9c0SRobert Millan 	return (ioctl(disk->d_fd, DIOCGDELETE, ioarg));
134bcedb9c0SRobert Millan }
135bcedb9c0SRobert Millan 
136bcedb9c0SRobert Millan #else
137bcedb9c0SRobert Millan 
138bcedb9c0SRobert Millan static int
berase_helper(struct uufsd * disk,ufs2_daddr_t blockno,ufs2_daddr_t size)139bcedb9c0SRobert Millan berase_helper(struct uufsd *disk, ufs2_daddr_t blockno, ufs2_daddr_t size)
140bcedb9c0SRobert Millan {
141bcedb9c0SRobert Millan 	char *zero_chunk;
142bcedb9c0SRobert Millan 	off_t offset, zero_chunk_size, pwrite_size;
143bcedb9c0SRobert Millan 	int rv;
144bcedb9c0SRobert Millan 
145bcedb9c0SRobert Millan 	offset = blockno * disk->d_bsize;
146bcedb9c0SRobert Millan 	zero_chunk_size = 65536 * disk->d_bsize;
147bcedb9c0SRobert Millan 	zero_chunk = calloc(1, zero_chunk_size);
148bcedb9c0SRobert Millan 	if (zero_chunk == NULL) {
149bcedb9c0SRobert Millan 		ERROR(disk, "failed to allocate memory");
150bcedb9c0SRobert Millan 		return (-1);
151bcedb9c0SRobert Millan 	}
152bcedb9c0SRobert Millan 	while (size > 0) {
153bcedb9c0SRobert Millan 		pwrite_size = size;
154bcedb9c0SRobert Millan 		if (pwrite_size > zero_chunk_size)
155bcedb9c0SRobert Millan 			pwrite_size = zero_chunk_size;
156bcedb9c0SRobert Millan 		rv = pwrite(disk->d_fd, zero_chunk, pwrite_size, offset);
157bcedb9c0SRobert Millan 		if (rv == -1) {
158bcedb9c0SRobert Millan 			ERROR(disk, "failed writing to disk");
159bcedb9c0SRobert Millan 			break;
160bcedb9c0SRobert Millan 		}
161bcedb9c0SRobert Millan 		size -= rv;
162bcedb9c0SRobert Millan 		offset += rv;
163bcedb9c0SRobert Millan 		rv = 0;
164bcedb9c0SRobert Millan 	}
165bcedb9c0SRobert Millan 	free(zero_chunk);
166bcedb9c0SRobert Millan 	return (rv);
167bcedb9c0SRobert Millan }
168bcedb9c0SRobert Millan 
169bcedb9c0SRobert Millan #endif
170bcedb9c0SRobert Millan 
17120a0f65bSPoul-Henning Kamp int
berase(struct uufsd * disk,ufs2_daddr_t blockno,ufs2_daddr_t size)17220a0f65bSPoul-Henning Kamp berase(struct uufsd *disk, ufs2_daddr_t blockno, ufs2_daddr_t size)
17320a0f65bSPoul-Henning Kamp {
17420a0f65bSPoul-Henning Kamp 	int rv;
17520a0f65bSPoul-Henning Kamp 
17620a0f65bSPoul-Henning Kamp 	ERROR(disk, NULL);
17720a0f65bSPoul-Henning Kamp 	rv = ufs_disk_write(disk);
17820a0f65bSPoul-Henning Kamp 	if (rv == -1) {
17920a0f65bSPoul-Henning Kamp 		ERROR(disk, "failed to open disk for writing");
18020a0f65bSPoul-Henning Kamp 		return(rv);
18120a0f65bSPoul-Henning Kamp 	}
182bcedb9c0SRobert Millan 	return (berase_helper(disk, blockno, size));
18320a0f65bSPoul-Henning Kamp }
184