2 * Copyright (c) 2014 Juniper Networks, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include <sys/queue.h>
33 #include <sys/types.h>
48 STAILQ_ENTRY(chunk) ch_list;
49 size_t ch_size; /* Size of chunk in bytes. */
50 lba_t ch_block; /* Block address in image. */
53 off_t ofs; /* Offset in backing file. */
54 int fd; /* FD of backing file. */
57 void *ptr; /* Pointer to data in memory */
61 #define CH_TYPE_ZEROES 0 /* Chunk is a gap (no data). */
62 #define CH_TYPE_FILE 1 /* File-backed chunk. */
63 #define CH_TYPE_MEMORY 2 /* Memory-backed chunk */
66 static STAILQ_HEAD(chunk_head, chunk) image_chunks;
67 static u_int image_nchunks;
69 static char image_swap_file[PATH_MAX];
70 static int image_swap_fd = -1;
71 static u_int image_swap_pgsz;
72 static off_t image_swap_size;
74 static lba_t image_size;
77 is_empty_sector(void *buf)
82 assert(((uintptr_t)p & 3) == 0);
84 max = secsz / sizeof(uint64_t);
85 for (n = 0; n < max; n++) {
97 image_swap_alloc(size_t size)
102 unit = (secsz > image_swap_pgsz) ? secsz : image_swap_pgsz;
103 assert((unit & (unit - 1)) == 0);
105 size = (size + unit - 1) & ~(unit - 1);
107 ofs = image_swap_size;
108 image_swap_size += size;
109 if (ftruncate(image_swap_fd, image_swap_size) == -1) {
110 image_swap_size = ofs;
117 * Image chunk handling.
120 static struct chunk *
121 image_chunk_find(lba_t blk)
123 static struct chunk *last = NULL;
126 ch = (last != NULL && last->ch_block <= blk)
127 ? last : STAILQ_FIRST(&image_chunks);
129 if (ch->ch_block <= blk &&
130 (lba_t)(ch->ch_block + (ch->ch_size / secsz)) > blk) {
134 ch = STAILQ_NEXT(ch, ch_list);
140 image_chunk_grow(struct chunk *ch, size_t sz)
144 newsz = ch->ch_size + sz;
145 if (newsz > ch->ch_size) {
149 /* We would overflow -- create new chunk for remainder. */
150 dsz = SIZE_MAX - ch->ch_size;
152 ch->ch_size = SIZE_MAX;
156 static struct chunk *
157 image_chunk_memory(struct chunk *ch, lba_t blk)
162 ptr = calloc(1, secsz);
166 if (ch->ch_block < blk) {
167 new = malloc(sizeof(*new));
172 memcpy(new, ch, sizeof(*new));
173 ch->ch_size = (blk - ch->ch_block) * secsz;
175 new->ch_size -= ch->ch_size;
176 STAILQ_INSERT_AFTER(&image_chunks, ch, new, ch_list);
181 if (ch->ch_size > secsz) {
182 new = malloc(sizeof(*new));
187 memcpy(new, ch, sizeof(*new));
190 new->ch_size -= secsz;
191 STAILQ_INSERT_AFTER(&image_chunks, ch, new, ch_list);
195 ch->ch_type = CH_TYPE_MEMORY;
196 ch->ch_u.mem.ptr = ptr;
201 image_chunk_skipto(lba_t to)
207 ch = STAILQ_LAST(&image_chunks, chunk, ch_list);
208 from = (ch != NULL) ? ch->ch_block + (ch->ch_size / secsz) : 0LL;
215 /* Avoid bugs due to overflows. */
216 if ((uintmax_t)(to - from) > (uintmax_t)(SIZE_MAX / secsz))
218 sz = (to - from) * secsz;
219 if (ch != NULL && ch->ch_type == CH_TYPE_ZEROES) {
220 sz = image_chunk_grow(ch, sz);
223 from = ch->ch_block + (ch->ch_size / secsz);
225 ch = malloc(sizeof(*ch));
228 memset(ch, 0, sizeof(*ch));
231 ch->ch_type = CH_TYPE_ZEROES;
232 STAILQ_INSERT_TAIL(&image_chunks, ch, ch_list);
238 image_chunk_append(lba_t blk, size_t sz, off_t ofs, int fd)
242 ch = STAILQ_LAST(&image_chunks, chunk, ch_list);
243 if (ch != NULL && ch->ch_type == CH_TYPE_FILE) {
244 if (fd == ch->ch_u.file.fd &&
245 blk == (lba_t)(ch->ch_block + (ch->ch_size / secsz)) &&
246 ofs == (off_t)(ch->ch_u.file.ofs + ch->ch_size)) {
247 sz = image_chunk_grow(ch, sz);
250 blk = ch->ch_block + (ch->ch_size / secsz);
251 ofs = ch->ch_u.file.ofs + ch->ch_size;
254 ch = malloc(sizeof(*ch));
257 memset(ch, 0, sizeof(*ch));
260 ch->ch_type = CH_TYPE_FILE;
261 ch->ch_u.file.ofs = ofs;
262 ch->ch_u.file.fd = fd;
263 STAILQ_INSERT_TAIL(&image_chunks, ch, ch_list);
269 image_chunk_copyin(lba_t blk, void *buf, size_t sz, off_t ofs, int fd)
275 sz = (sz + secsz - 1) & ~(secsz - 1);
276 while (!error && sz > 0) {
277 if (is_empty_sector(p))
278 error = image_chunk_skipto(blk + 1);
280 error = image_chunk_append(blk, secsz, ofs, fd);
290 * File mapping support.
294 image_file_map(int fd, off_t ofs, size_t sz)
300 unit = (secsz > image_swap_pgsz) ? secsz : image_swap_pgsz;
301 assert((unit & (unit - 1)) == 0);
303 flags = MAP_NOCORE | MAP_NOSYNC | MAP_SHARED;
304 /* Allow writing to our swap file only. */
305 prot = PROT_READ | ((fd == image_swap_fd) ? PROT_WRITE : 0);
306 sz = (sz + unit - 1) & ~(unit - 1);
307 ptr = mmap(NULL, sz, prot, flags, fd, ofs);
308 return ((ptr == MAP_FAILED) ? NULL : ptr);
312 image_file_unmap(void *buffer, size_t sz)
316 unit = (secsz > image_swap_pgsz) ? secsz : image_swap_pgsz;
317 sz = (sz + unit - 1) & ~(unit - 1);
323 * Input/source file handling.
327 image_copyin_stream(lba_t blk, int fd, uint64_t *sizep)
337 * This makes sure we're doing I/O in multiples of the page
338 * size as well as of the sector size. 2MB is the minimum
339 * by virtue of secsz at least 512 bytes and the page size
342 iosz = secsz * image_swap_pgsz;
346 swofs = image_swap_alloc(iosz);
349 buffer = image_file_map(image_swap_fd, swofs, iosz);
352 rdsz = read(fd, buffer, iosz);
354 error = image_chunk_copyin(blk, buffer, rdsz, swofs,
360 image_file_unmap(buffer, iosz);
361 /* XXX should we relinguish unused swap space? */
366 blk += (rdsz + secsz - 1) / secsz;
375 image_copyin_mapped(lba_t blk, int fd, uint64_t *sizep)
377 off_t cur, data, end, hole, pos;
384 * We'd like to know the size of the file and we must
385 * be able to seek in order to mmap(2). If this isn't
386 * possible, then treat the file as a stream/pipe.
388 end = lseek(fd, 0L, SEEK_END);
390 return (image_copyin_stream(blk, fd, sizep));
393 * We need the file opened for the duration and our
394 * caller is going to close the file. Make a dup(2)
395 * so that control the faith of the descriptor.
401 iosz = secsz * image_swap_pgsz;
406 while (!error && cur < end) {
407 hole = lseek(fd, cur, SEEK_HOLE);
410 data = lseek(fd, cur, SEEK_DATA);
415 * Treat the entire file as data if sparse files
416 * are not supported by the underlying file system.
418 if (hole == end && data == end)
421 if (cur == hole && data > hole) {
423 pos = data & ~((uint64_t)secsz - 1);
425 blk += (pos - hole) / secsz;
426 error = image_chunk_skipto(blk);
428 bytesize += pos - hole;
430 } else if (cur == data && hole > data) {
432 pos = (hole + secsz - 1) & ~((uint64_t)secsz - 1);
435 sz = (pos - data > (off_t)iosz)
436 ? iosz : (size_t)(pos - data);
438 buf = image_file_map(fd, data, sz);
440 error = image_chunk_copyin(blk, buf,
442 image_file_unmap(buf, sz);
453 * I don't know what this means or whether it
454 * can happen at all...
462 if (!error && sizep != NULL)
468 image_copyin(lba_t blk, int fd, uint64_t *sizep)
473 error = image_chunk_skipto(blk);
475 if (fstat(fd, &sb) == -1 || !S_ISREG(sb.st_mode))
476 error = image_copyin_stream(blk, fd, sizep);
478 error = image_copyin_mapped(blk, fd, sizep);
484 * Output/sink file handling.
488 image_copyout(int fd)
492 error = image_copyout_region(fd, 0, image_size);
494 error = image_copyout_done(fd);
499 image_copyout_done(int fd)
504 ofs = lseek(fd, 0L, SEEK_CUR);
507 error = (ftruncate(fd, ofs) == -1) ? errno : 0;
512 image_copyout_memory(int fd, size_t size, void *ptr)
515 if (write(fd, ptr, size) == -1)
521 image_copyout_zeroes(int fd, size_t size)
523 static uint8_t *zeroes = NULL;
527 if (lseek(fd, (off_t)size, SEEK_CUR) != -1)
531 * If we can't seek, we must write.
534 if (zeroes == NULL) {
535 zeroes = calloc(1, secsz);
541 sz = (size > secsz) ? secsz : size;
542 error = image_copyout_memory(fd, sz, zeroes);
551 image_copyout_file(int fd, size_t size, int ifd, off_t iofs)
557 iosz = secsz * image_swap_pgsz;
560 sz = (size > iosz) ? iosz : size;
561 buf = image_file_map(ifd, iofs, sz);
564 error = image_copyout_memory(fd, sz, buf);
565 image_file_unmap(buf, sz);
575 image_copyout_region(int fd, lba_t blk, lba_t size)
584 ch = image_chunk_find(blk);
587 ofs = (blk - ch->ch_block) * secsz;
588 sz = ch->ch_size - ofs;
589 sz = ((lba_t)sz < size) ? sz : (size_t)size;
590 switch (ch->ch_type) {
592 error = image_copyout_zeroes(fd, sz);
595 error = image_copyout_file(fd, sz, ch->ch_u.file.fd,
596 ch->ch_u.file.ofs + ofs);
599 error = image_copyout_memory(fd, sz, ch->ch_u.mem.ptr);
611 image_data(lba_t blk, lba_t size)
617 ch = image_chunk_find(blk);
620 if (ch->ch_type != CH_TYPE_ZEROES)
622 lim = ch->ch_block + (ch->ch_size / secsz);
623 if (lim >= blk + size)
639 image_set_size(lba_t blk)
643 error = image_chunk_skipto(blk);
650 image_write(lba_t blk, void *buf, ssize_t len)
655 if (!is_empty_sector(buf)) {
656 ch = image_chunk_find(blk);
659 /* We may not be able to write to files. */
660 if (ch->ch_type == CH_TYPE_FILE)
662 if (ch->ch_type == CH_TYPE_ZEROES) {
663 ch = image_chunk_memory(ch, blk);
667 assert(ch->ch_type == CH_TYPE_MEMORY);
668 memcpy(ch->ch_u.mem.ptr, buf, secsz);
671 buf = (char *)buf + secsz;
682 while ((ch = STAILQ_FIRST(&image_chunks)) != NULL) {
683 switch (ch->ch_type) {
685 /* We may be closing the same file multiple times. */
686 if (ch->ch_u.file.fd != -1)
687 close(ch->ch_u.file.fd);
690 free(ch->ch_u.mem.ptr);
695 STAILQ_REMOVE_HEAD(&image_chunks, ch_list);
698 if (image_swap_fd != -1)
699 close(image_swap_fd);
700 unlink(image_swap_file);
708 STAILQ_INIT(&image_chunks);
712 image_swap_pgsz = getpagesize();
714 if (atexit(image_cleanup) == -1)
716 if ((tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0')
718 snprintf(image_swap_file, sizeof(image_swap_file), "%s/mkimg-XXXXXX",
720 image_swap_fd = mkstemp(image_swap_file);
721 if (image_swap_fd == -1)