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$");
61 TAILQ_ENTRY(chunk) ch_list;
62 size_t ch_size; /* Size of chunk in bytes. */
63 lba_t ch_block; /* Block address in image. */
66 off_t ofs; /* Offset in backing file. */
67 int fd; /* FD of backing file. */
70 void *ptr; /* Pointer to data in memory */
74 #define CH_TYPE_ZEROES 0 /* Chunk is a gap (no data). */
75 #define CH_TYPE_FILE 1 /* File-backed chunk. */
76 #define CH_TYPE_MEMORY 2 /* Memory-backed chunk */
79 static TAILQ_HEAD(chunk_head, chunk) image_chunks;
80 static u_int image_nchunks;
82 static char image_swap_file[PATH_MAX];
83 static int image_swap_fd = -1;
84 static u_int image_swap_pgsz;
85 static off_t image_swap_size;
87 static lba_t image_size;
90 is_empty_sector(void *buf)
95 assert(((uintptr_t)p & 3) == 0);
97 max = secsz / sizeof(uint64_t);
98 for (n = 0; n < max; n++) {
110 image_swap_alloc(size_t size)
115 unit = (secsz > image_swap_pgsz) ? secsz : image_swap_pgsz;
116 assert((unit & (unit - 1)) == 0);
118 size = (size + unit - 1) & ~(unit - 1);
120 ofs = image_swap_size;
121 image_swap_size += size;
122 if (ftruncate(image_swap_fd, image_swap_size) == -1) {
123 image_swap_size = ofs;
130 * Image chunk handling.
133 static struct chunk *
134 image_chunk_find(lba_t blk)
136 static struct chunk *last = NULL;
139 ch = (last != NULL && last->ch_block <= blk)
140 ? last : TAILQ_FIRST(&image_chunks);
142 if (ch->ch_block <= blk &&
143 (lba_t)(ch->ch_block + (ch->ch_size / secsz)) > blk) {
147 ch = TAILQ_NEXT(ch, ch_list);
153 image_chunk_grow(struct chunk *ch, size_t sz)
157 newsz = ch->ch_size + sz;
158 if (newsz > ch->ch_size) {
162 /* We would overflow -- create new chunk for remainder. */
163 dsz = SIZE_MAX - ch->ch_size;
165 ch->ch_size = SIZE_MAX;
169 static struct chunk *
170 image_chunk_memory(struct chunk *ch, lba_t blk)
175 ptr = calloc(1, secsz);
179 if (ch->ch_block < blk) {
180 new = malloc(sizeof(*new));
185 memcpy(new, ch, sizeof(*new));
186 ch->ch_size = (blk - ch->ch_block) * secsz;
188 new->ch_size -= ch->ch_size;
189 TAILQ_INSERT_AFTER(&image_chunks, ch, new, ch_list);
194 if (ch->ch_size > secsz) {
195 new = malloc(sizeof(*new));
200 memcpy(new, ch, sizeof(*new));
203 new->ch_size -= secsz;
204 TAILQ_INSERT_AFTER(&image_chunks, ch, new, ch_list);
208 ch->ch_type = CH_TYPE_MEMORY;
209 ch->ch_u.mem.ptr = ptr;
214 image_chunk_skipto(lba_t to)
220 ch = TAILQ_LAST(&image_chunks, chunk_head);
221 from = (ch != NULL) ? ch->ch_block + (ch->ch_size / secsz) : 0LL;
228 /* Avoid bugs due to overflows. */
229 if ((uintmax_t)(to - from) > (uintmax_t)(SIZE_MAX / secsz))
231 sz = (to - from) * secsz;
232 if (ch != NULL && ch->ch_type == CH_TYPE_ZEROES) {
233 sz = image_chunk_grow(ch, sz);
236 from = ch->ch_block + (ch->ch_size / secsz);
238 ch = malloc(sizeof(*ch));
241 memset(ch, 0, sizeof(*ch));
244 ch->ch_type = CH_TYPE_ZEROES;
245 TAILQ_INSERT_TAIL(&image_chunks, ch, ch_list);
251 image_chunk_append(lba_t blk, size_t sz, off_t ofs, int fd)
255 ch = TAILQ_LAST(&image_chunks, chunk_head);
256 if (ch != NULL && ch->ch_type == CH_TYPE_FILE) {
257 if (fd == ch->ch_u.file.fd &&
258 blk == (lba_t)(ch->ch_block + (ch->ch_size / secsz)) &&
259 ofs == (off_t)(ch->ch_u.file.ofs + ch->ch_size)) {
260 sz = image_chunk_grow(ch, sz);
263 blk = ch->ch_block + (ch->ch_size / secsz);
264 ofs = ch->ch_u.file.ofs + ch->ch_size;
267 ch = malloc(sizeof(*ch));
270 memset(ch, 0, sizeof(*ch));
273 ch->ch_type = CH_TYPE_FILE;
274 ch->ch_u.file.ofs = ofs;
275 ch->ch_u.file.fd = fd;
276 TAILQ_INSERT_TAIL(&image_chunks, ch, ch_list);
282 image_chunk_copyin(lba_t blk, void *buf, size_t sz, off_t ofs, int fd)
288 sz = (sz + secsz - 1) & ~(secsz - 1);
289 while (!error && sz > 0) {
290 if (is_empty_sector(p))
291 error = image_chunk_skipto(blk + 1);
293 error = image_chunk_append(blk, secsz, ofs, fd);
303 * File mapping support.
307 image_file_map(int fd, off_t ofs, size_t sz)
313 unit = (secsz > image_swap_pgsz) ? secsz : image_swap_pgsz;
314 assert((unit & (unit - 1)) == 0);
316 flags = MAP_NOCORE | MAP_NOSYNC | MAP_SHARED;
317 /* Allow writing to our swap file only. */
318 prot = PROT_READ | ((fd == image_swap_fd) ? PROT_WRITE : 0);
319 sz = (sz + unit - 1) & ~(unit - 1);
320 ptr = mmap(NULL, sz, prot, flags, fd, ofs);
321 return ((ptr == MAP_FAILED) ? NULL : ptr);
325 image_file_unmap(void *buffer, size_t sz)
329 unit = (secsz > image_swap_pgsz) ? secsz : image_swap_pgsz;
330 sz = (sz + unit - 1) & ~(unit - 1);
331 if (madvise(buffer, sz, MADV_DONTNEED) != 0)
338 * Input/source file handling.
342 image_copyin_stream(lba_t blk, int fd, uint64_t *sizep)
352 * This makes sure we're doing I/O in multiples of the page
353 * size as well as of the sector size. 2MB is the minimum
354 * by virtue of secsz at least 512 bytes and the page size
357 iosz = secsz * image_swap_pgsz;
361 swofs = image_swap_alloc(iosz);
364 buffer = image_file_map(image_swap_fd, swofs, iosz);
367 rdsz = read(fd, buffer, iosz);
369 error = image_chunk_copyin(blk, buffer, rdsz, swofs,
375 image_file_unmap(buffer, iosz);
376 /* XXX should we relinguish unused swap space? */
381 blk += (rdsz + secsz - 1) / secsz;
390 image_copyin_mapped(lba_t blk, int fd, uint64_t *sizep)
392 off_t cur, data, end, hole, pos;
399 * We'd like to know the size of the file and we must
400 * be able to seek in order to mmap(2). If this isn't
401 * possible, then treat the file as a stream/pipe.
403 end = lseek(fd, 0L, SEEK_END);
405 return (image_copyin_stream(blk, fd, sizep));
408 * We need the file opened for the duration and our
409 * caller is going to close the file. Make a dup(2)
410 * so that control the faith of the descriptor.
416 iosz = secsz * image_swap_pgsz;
421 while (!error && cur < end) {
422 hole = lseek(fd, cur, SEEK_HOLE);
425 data = lseek(fd, cur, SEEK_DATA);
430 * Treat the entire file as data if sparse files
431 * are not supported by the underlying file system.
433 if (hole == end && data == end)
436 if (cur == hole && data > hole) {
438 pos = data & ~((uint64_t)secsz - 1);
440 blk += (pos - hole) / secsz;
441 error = image_chunk_skipto(blk);
443 bytesize += pos - hole;
445 } else if (cur == data && hole > data) {
447 pos = (hole + secsz - 1) & ~((uint64_t)secsz - 1);
450 sz = (pos - data > (off_t)iosz)
451 ? iosz : (size_t)(pos - data);
453 buf = image_file_map(fd, data, sz);
455 error = image_chunk_copyin(blk, buf,
457 image_file_unmap(buf, sz);
468 * I don't know what this means or whether it
469 * can happen at all...
476 if (!error && sizep != NULL)
482 image_copyin(lba_t blk, int fd, uint64_t *sizep)
487 error = image_chunk_skipto(blk);
489 if (fstat(fd, &sb) == -1 || !S_ISREG(sb.st_mode))
490 error = image_copyin_stream(blk, fd, sizep);
492 error = image_copyin_mapped(blk, fd, sizep);
498 * Output/sink file handling.
502 image_copyout(int fd)
506 error = image_copyout_region(fd, 0, image_size);
508 error = image_copyout_done(fd);
513 image_copyout_done(int fd)
518 ofs = lseek(fd, 0L, SEEK_CUR);
521 error = (ftruncate(fd, ofs) == -1) ? errno : 0;
526 image_copyout_memory(int fd, size_t size, void *ptr)
529 if (write(fd, ptr, size) == -1)
535 image_copyout_zeroes(int fd, size_t count)
537 static uint8_t *zeroes = NULL;
541 if (lseek(fd, (off_t)count, SEEK_CUR) != -1)
545 * If we can't seek, we must write.
548 if (zeroes == NULL) {
549 zeroes = calloc(1, secsz);
555 sz = (count > secsz) ? secsz : count;
556 error = image_copyout_memory(fd, sz, zeroes);
565 image_copyout_file(int fd, size_t size, int ifd, off_t iofs)
571 iosz = secsz * image_swap_pgsz;
574 sz = (size > iosz) ? iosz : size;
575 buf = image_file_map(ifd, iofs, sz);
578 error = image_copyout_memory(fd, sz, buf);
579 image_file_unmap(buf, sz);
589 image_copyout_region(int fd, lba_t blk, lba_t size)
598 while (!error && size > 0) {
599 ch = image_chunk_find(blk);
604 ofs = (blk - ch->ch_block) * secsz;
605 sz = ch->ch_size - ofs;
606 sz = ((lba_t)sz < size) ? sz : (size_t)size;
607 switch (ch->ch_type) {
609 error = image_copyout_zeroes(fd, sz);
612 error = image_copyout_file(fd, sz, ch->ch_u.file.fd,
613 ch->ch_u.file.ofs + ofs);
616 error = image_copyout_memory(fd, sz, ch->ch_u.mem.ptr);
628 image_data(lba_t blk, lba_t size)
634 ch = image_chunk_find(blk);
637 if (ch->ch_type != CH_TYPE_ZEROES)
639 lim = ch->ch_block + (ch->ch_size / secsz);
640 if (lim >= blk + size)
656 image_set_size(lba_t blk)
660 error = image_chunk_skipto(blk);
667 image_write(lba_t blk, void *buf, ssize_t len)
672 if (!is_empty_sector(buf)) {
673 ch = image_chunk_find(blk);
676 /* We may not be able to write to files. */
677 if (ch->ch_type == CH_TYPE_FILE)
679 if (ch->ch_type == CH_TYPE_ZEROES) {
680 ch = image_chunk_memory(ch, blk);
684 assert(ch->ch_type == CH_TYPE_MEMORY);
685 memcpy(ch->ch_u.mem.ptr, buf, secsz);
688 buf = (char *)buf + secsz;
699 while ((ch = TAILQ_FIRST(&image_chunks)) != NULL) {
700 switch (ch->ch_type) {
702 /* We may be closing the same file multiple times. */
703 if (ch->ch_u.file.fd != -1)
704 close(ch->ch_u.file.fd);
707 free(ch->ch_u.mem.ptr);
712 TAILQ_REMOVE(&image_chunks, ch, ch_list);
715 if (image_swap_fd != -1)
716 close(image_swap_fd);
717 unlink(image_swap_file);
725 TAILQ_INIT(&image_chunks);
729 image_swap_pgsz = getpagesize();
731 if (atexit(image_cleanup) == -1)
733 if ((tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0')
735 snprintf(image_swap_file, sizeof(image_swap_file), "%s/mkimg-XXXXXX",
737 image_swap_fd = mkstemp(image_swap_file);
738 if (image_swap_fd == -1)