Update Red Hat Copyright Notices
[nbdkit.git] / filters / partition / partition.c
blobb206f48c8670f16e38e646d2644015fa830fd599
1 /* nbdkit
2 * Copyright Red Hat
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * * Neither the name of Red Hat nor the names of its contributors may be
16 * used to endorse or promote products derived from this software without
17 * specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #include <config.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <string.h>
39 #include <inttypes.h>
40 #include <assert.h>
42 #include <nbdkit-filter.h>
44 #include "byte-swapping.h"
45 #include "cleanup.h"
47 #include "partition.h"
49 unsigned partnum = 0;
51 /* Called for each key=value passed on the command line. */
52 static int
53 partition_config (nbdkit_next_config *next, nbdkit_backend *nxdata,
54 const char *key, const char *value)
56 if (strcmp (key, "partition") == 0) {
57 if (nbdkit_parse_unsigned ("partition", value, &partnum) == -1)
58 return -1;
59 if (partnum == 0) {
60 nbdkit_error ("invalid partition number");
61 return -1;
63 return 0;
65 else
66 return next (nxdata, key, value);
69 /* Check the user did pass partition number. */
70 static int
71 partition_config_complete (nbdkit_next_config_complete *next,
72 nbdkit_backend *nxdata)
74 if (partnum == 0) {
75 nbdkit_error ("you must supply the partition parameter on the command line");
76 return -1;
79 return next (nxdata);
82 #define partition_config_help \
83 "partition=<PART> (required) The partition number (counting from 1)."
85 struct handle {
86 int64_t offset;
87 int64_t range;
88 const char *type;
91 /* Open a connection. */
92 static void *
93 partition_open (nbdkit_next_open *next, nbdkit_context *nxdata,
94 int readonly, const char *exportname, int is_tls)
96 struct handle *h;
98 if (next (nxdata, readonly, exportname) == -1)
99 return NULL;
101 h = malloc (sizeof *h);
102 if (h == NULL) {
103 nbdkit_error ("malloc: %m");
104 return NULL;
107 /* These are set in the prepare method. */
108 h->offset = h->range = -1;
109 return h;
112 static void
113 partition_close (void *handle)
115 struct handle *h = handle;
117 free (h);
120 static int
121 partition_prepare (nbdkit_next *next,
122 void *handle, int readonly)
124 struct handle *h = handle;
125 int64_t size;
126 uint8_t lba01[2*SECTOR_SIZE]; /* LBA 0 and 1 */
127 int r;
128 int err;
130 size = next->get_size (next);
131 if (size == -1)
132 return -1;
133 if (size < 2 * SECTOR_SIZE) {
134 nbdkit_error ("disk is too small to be a partitioned disk");
135 return -1;
138 nbdkit_debug ("disk size=%" PRIi64, size);
140 if (next->pread (next, lba01, sizeof lba01, 0, 0, &err) == -1)
141 return -1;
143 /* Is it GPT? */
144 if (size >= 2 * 34 * SECTOR_SIZE &&
145 memcmp (&lba01[SECTOR_SIZE], "EFI PART", 8) == 0) {
146 r = find_gpt_partition (next, size, &lba01[SECTOR_SIZE], &h->offset,
147 &h->range);
148 h->type = "GPT";
150 /* Is it MBR? */
151 else if (lba01[0x1fe] == 0x55 && lba01[0x1ff] == 0xAA) {
152 r = find_mbr_partition (next, size, lba01, &h->offset, &h->range);
153 h->type = "MBR";
155 else {
156 nbdkit_error ("disk does not contain MBR or GPT partition table signature");
157 r = -1;
159 if (r == -1)
160 return -1;
162 /* The find_*_partition functions set h->offset & h->range to the
163 * partition boundaries. We additionally check that they are inside
164 * the underlying disk.
166 if (h->offset < 0 || h->range < 0 || h->offset + h->range > size) {
167 nbdkit_error ("partition is outside the disk");
168 return -1;
171 nbdkit_debug ("partition offset=%" PRIi64 " range=%" PRIi64,
172 h->offset, h->range);
174 return 0;
177 /* Description. */
178 static const char *
179 partition_export_description (nbdkit_next *next,
180 void *handle)
182 struct handle *h = handle;
183 const char *base = next->export_description (next);
185 assert (h->type);
186 if (!base)
187 return NULL;
188 return nbdkit_printf_intern ("partition %d of %s disk: %s", partnum, h->type,
189 base);
192 /* Get the file size. */
193 static int64_t
194 partition_get_size (nbdkit_next *next,
195 void *handle)
197 struct handle *h = handle;
199 return h->range;
202 /* Read data. */
203 static int
204 partition_pread (nbdkit_next *next,
205 void *handle, void *buf, uint32_t count, uint64_t offs,
206 uint32_t flags, int *err)
208 struct handle *h = handle;
210 return next->pread (next, buf, count, offs + h->offset, flags, err);
213 /* Write data. */
214 static int
215 partition_pwrite (nbdkit_next *next,
216 void *handle,
217 const void *buf, uint32_t count, uint64_t offs,
218 uint32_t flags, int *err)
220 struct handle *h = handle;
222 return next->pwrite (next, buf, count, offs + h->offset, flags, err);
225 /* Trim data. */
226 static int
227 partition_trim (nbdkit_next *next,
228 void *handle, uint32_t count, uint64_t offs, uint32_t flags,
229 int *err)
231 struct handle *h = handle;
233 return next->trim (next, count, offs + h->offset, flags, err);
236 /* Zero data. */
237 static int
238 partition_zero (nbdkit_next *next,
239 void *handle, uint32_t count, uint64_t offs, uint32_t flags,
240 int *err)
242 struct handle *h = handle;
244 return next->zero (next, count, offs + h->offset, flags, err);
247 /* Extents. */
248 static int
249 partition_extents (nbdkit_next *next,
250 void *handle, uint32_t count, uint64_t offs, uint32_t flags,
251 struct nbdkit_extents *extents, int *err)
253 struct handle *h = handle;
254 size_t i;
255 CLEANUP_EXTENTS_FREE struct nbdkit_extents *extents2 = NULL;
256 struct nbdkit_extent e;
258 extents2 = nbdkit_extents_new (offs + h->offset, h->offset + h->range);
259 if (extents2 == NULL) {
260 *err = errno;
261 return -1;
263 if (next->extents (next, count, offs + h->offset, flags, extents2,
264 err) == -1)
265 return -1;
267 for (i = 0; i < nbdkit_extents_count (extents2); ++i) {
268 e = nbdkit_get_extent (extents2, i);
269 e.offset -= h->offset;
270 if (nbdkit_add_extent (extents, e.offset, e.length, e.type) == -1) {
271 *err = errno;
272 return -1;
275 return 0;
278 /* Cache data. */
279 static int
280 partition_cache (nbdkit_next *next,
281 void *handle, uint32_t count, uint64_t offs, uint32_t flags,
282 int *err)
284 struct handle *h = handle;
286 return next->cache (next, count, offs + h->offset, flags, err);
289 static struct nbdkit_filter filter = {
290 .name = "partition",
291 .longname = "nbdkit partition filter",
292 .config = partition_config,
293 .config_complete = partition_config_complete,
294 .config_help = partition_config_help,
295 .open = partition_open,
296 .prepare = partition_prepare,
297 .close = partition_close,
298 .export_description = partition_export_description,
299 .get_size = partition_get_size,
300 .pread = partition_pread,
301 .pwrite = partition_pwrite,
302 .trim = partition_trim,
303 .zero = partition_zero,
304 .extents = partition_extents,
305 .cache = partition_cache,
308 NBDKIT_REGISTER_FILTER (filter)