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