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>
49 STAILQ_ENTRY(chunk) ch_list;
50 size_t ch_size; /* Size of chunk in bytes. */
51 lba_t ch_block; /* Block address in image. */
54 off_t ofs; /* Offset in backing file. */
55 int fd; /* FD of backing file. */
58 void *ptr; /* Pointer to data in memory */
62 #define CH_TYPE_ZEROES 0 /* Chunk is a gap (no data). */
63 #define CH_TYPE_FILE 1 /* File-backed chunk. */
64 #define CH_TYPE_MEMORY 2 /* Memory-backed chunk */
67 static STAILQ_HEAD(chunk_head, chunk) image_chunks;
68 static u_int image_nchunks;
70 static char image_swap_file[PATH_MAX];
71 static int image_swap_fd = -1;
72 static u_int image_swap_pgsz;
73 static off_t image_swap_size;
75 static lba_t image_size;
78 is_empty_sector(void *buf)
83 assert(((uintptr_t)p & 3) == 0);
85 max = secsz / sizeof(uint64_t);
86 for (n = 0; n < max; n++) {
98 image_swap_alloc(size_t size)
103 unit = (secsz > image_swap_pgsz) ? secsz : image_swap_pgsz;
104 assert((unit & (unit - 1)) == 0);
106 size = (size + unit - 1) & ~(unit - 1);
108 ofs = image_swap_size;
109 image_swap_size += size;
110 if (ftruncate(image_swap_fd, image_swap_size) == -1) {
111 image_swap_size = ofs;
118 * Image chunk handling.
121 static struct chunk *
122 image_chunk_find(lba_t blk)
124 static struct chunk *last = NULL;
127 ch = (last != NULL && last->ch_block <= blk)
128 ? last : STAILQ_FIRST(&image_chunks);
130 if (ch->ch_block <= blk &&
131 (lba_t)(ch->ch_block + (ch->ch_size / secsz)) > blk) {
135 ch = STAILQ_NEXT(ch, ch_list);
141 image_chunk_grow(struct chunk *ch, size_t sz)
145 newsz = ch->ch_size + sz;
146 if (newsz > ch->ch_size) {
150 /* We would overflow -- create new chunk for remainder. */
151 dsz = SIZE_MAX - ch->ch_size;
153 ch->ch_size = SIZE_MAX;
157 static struct chunk *
158 image_chunk_memory(struct chunk *ch, lba_t blk)
163 ptr = calloc(1, secsz);
167 if (ch->ch_block < blk) {
168 new = malloc(sizeof(*new));
173 memcpy(new, ch, sizeof(*new));
174 ch->ch_size = (blk - ch->ch_block) * secsz;
176 new->ch_size -= ch->ch_size;
177 STAILQ_INSERT_AFTER(&image_chunks, ch, new, ch_list);
182 if (ch->ch_size > secsz) {
183 new = malloc(sizeof(*new));
188 memcpy(new, ch, sizeof(*new));
191 new->ch_size -= secsz;
192 STAILQ_INSERT_AFTER(&image_chunks, ch, new, ch_list);
196 ch->ch_type = CH_TYPE_MEMORY;
197 ch->ch_u.mem.ptr = ptr;
202 image_chunk_skipto(lba_t to)
208 ch = STAILQ_LAST(&image_chunks, chunk, ch_list);
209 from = (ch != NULL) ? ch->ch_block + (ch->ch_size / secsz) : 0LL;
216 /* Avoid bugs due to overflows. */
217 if ((uintmax_t)(to - from) > (uintmax_t)(SIZE_MAX / secsz))
219 sz = (to - from) * secsz;
220 if (ch != NULL && ch->ch_type == CH_TYPE_ZEROES) {
221 sz = image_chunk_grow(ch, sz);
224 from = ch->ch_block + (ch->ch_size / secsz);
226 ch = malloc(sizeof(*ch));
229 memset(ch, 0, sizeof(*ch));
232 ch->ch_type = CH_TYPE_ZEROES;
233 STAILQ_INSERT_TAIL(&image_chunks, ch, ch_list);
239 image_chunk_append(lba_t blk, size_t sz, off_t ofs, int fd)
243 ch = STAILQ_LAST(&image_chunks, chunk, ch_list);
244 if (ch != NULL && ch->ch_type == CH_TYPE_FILE) {
245 if (fd == ch->ch_u.file.fd &&
246 blk == (lba_t)(ch->ch_block + (ch->ch_size / secsz)) &&
247 ofs == (off_t)(ch->ch_u.file.ofs + ch->ch_size)) {
248 sz = image_chunk_grow(ch, sz);
251 blk = ch->ch_block + (ch->ch_size / secsz);
252 ofs = ch->ch_u.file.ofs + ch->ch_size;
255 ch = malloc(sizeof(*ch));
258 memset(ch, 0, sizeof(*ch));
261 ch->ch_type = CH_TYPE_FILE;
262 ch->ch_u.file.ofs = ofs;
263 ch->ch_u.file.fd = fd;
264 STAILQ_INSERT_TAIL(&image_chunks, ch, ch_list);
270 image_chunk_copyin(lba_t blk, void *buf, size_t sz, off_t ofs, int fd)
276 sz = (sz + secsz - 1) & ~(secsz - 1);
277 while (!error && sz > 0) {
278 if (is_empty_sector(p))
279 error = image_chunk_skipto(blk + 1);
281 error = image_chunk_append(blk, secsz, ofs, fd);
291 * File mapping support.
295 image_file_map(int fd, off_t ofs, size_t sz)
301 unit = (secsz > image_swap_pgsz) ? secsz : image_swap_pgsz;
302 assert((unit & (unit - 1)) == 0);
304 flags = MAP_NOCORE | MAP_NOSYNC | MAP_SHARED;
305 /* Allow writing to our swap file only. */
306 prot = PROT_READ | ((fd == image_swap_fd) ? PROT_WRITE : 0);
307 sz = (sz + unit - 1) & ~(unit - 1);
308 ptr = mmap(NULL, sz, prot, flags, fd, ofs);
309 return ((ptr == MAP_FAILED) ? NULL : ptr);
313 image_file_unmap(void *buffer, size_t sz)
317 unit = (secsz > image_swap_pgsz) ? secsz : image_swap_pgsz;
318 sz = (sz + unit - 1) & ~(unit - 1);
319 if (madvise(buffer, sz, MADV_DONTNEED) != 0)
326 * Input/source file handling.
330 image_copyin_stream(lba_t blk, int fd, uint64_t *sizep)
340 * This makes sure we're doing I/O in multiples of the page
341 * size as well as of the sector size. 2MB is the minimum
342 * by virtue of secsz at least 512 bytes and the page size
345 iosz = secsz * image_swap_pgsz;
349 swofs = image_swap_alloc(iosz);
352 buffer = image_file_map(image_swap_fd, swofs, iosz);
355 rdsz = read(fd, buffer, iosz);
357 error = image_chunk_copyin(blk, buffer, rdsz, swofs,
363 image_file_unmap(buffer, iosz);
364 /* XXX should we relinguish unused swap space? */
369 blk += (rdsz + secsz - 1) / secsz;
378 image_copyin_mapped(lba_t blk, int fd, uint64_t *sizep)
380 off_t cur, data, end, hole, pos;
387 * We'd like to know the size of the file and we must
388 * be able to seek in order to mmap(2). If this isn't
389 * possible, then treat the file as a stream/pipe.
391 end = lseek(fd, 0L, SEEK_END);
393 return (image_copyin_stream(blk, fd, sizep));
396 * We need the file opened for the duration and our
397 * caller is going to close the file. Make a dup(2)
398 * so that control the faith of the descriptor.
404 iosz = secsz * image_swap_pgsz;
409 while (!error && cur < end) {
410 hole = lseek(fd, cur, SEEK_HOLE);
413 data = lseek(fd, cur, SEEK_DATA);
418 * Treat the entire file as data if sparse files
419 * are not supported by the underlying file system.
421 if (hole == end && data == end)
424 if (cur == hole && data > hole) {
426 pos = data & ~((uint64_t)secsz - 1);
428 blk += (pos - hole) / secsz;
429 error = image_chunk_skipto(blk);
431 bytesize += pos - hole;
433 } else if (cur == data && hole > data) {
435 pos = (hole + secsz - 1) & ~((uint64_t)secsz - 1);
438 sz = (pos - data > (off_t)iosz)
439 ? iosz : (size_t)(pos - data);
441 buf = image_file_map(fd, data, sz);
443 error = image_chunk_copyin(blk, buf,
445 image_file_unmap(buf, sz);
456 * I don't know what this means or whether it
457 * can happen at all...
465 if (!error && sizep != NULL)
471 image_copyin(lba_t blk, int fd, uint64_t *sizep)
476 error = image_chunk_skipto(blk);
478 if (fstat(fd, &sb) == -1 || !S_ISREG(sb.st_mode))
479 error = image_copyin_stream(blk, fd, sizep);
481 error = image_copyin_mapped(blk, fd, sizep);
487 * Output/sink file handling.
491 image_copyout(int fd)
495 error = image_copyout_region(fd, 0, image_size);
497 error = image_copyout_done(fd);
502 image_copyout_done(int fd)
507 ofs = lseek(fd, 0L, SEEK_CUR);
510 error = (ftruncate(fd, ofs) == -1) ? errno : 0;
515 image_copyout_memory(int fd, size_t size, void *ptr)
518 if (write(fd, ptr, size) == -1)
524 image_copyout_zeroes(int fd, size_t count)
526 static uint8_t *zeroes = NULL;
530 if (lseek(fd, (off_t)count, SEEK_CUR) != -1)
534 * If we can't seek, we must write.
537 if (zeroes == NULL) {
538 zeroes = calloc(1, secsz);
544 sz = (count > secsz) ? secsz : count;
545 error = image_copyout_memory(fd, sz, zeroes);
554 image_copyout_file(int fd, size_t size, int ifd, off_t iofs)
560 iosz = secsz * image_swap_pgsz;
563 sz = (size > iosz) ? iosz : size;
564 buf = image_file_map(ifd, iofs, sz);
567 error = image_copyout_memory(fd, sz, buf);
568 image_file_unmap(buf, sz);
578 image_copyout_region(int fd, lba_t blk, lba_t size)
587 ch = image_chunk_find(blk);
590 ofs = (blk - ch->ch_block) * secsz;
591 sz = ch->ch_size - ofs;
592 sz = ((lba_t)sz < size) ? sz : (size_t)size;
593 switch (ch->ch_type) {
595 error = image_copyout_zeroes(fd, sz);
598 error = image_copyout_file(fd, sz, ch->ch_u.file.fd,
599 ch->ch_u.file.ofs + ofs);
602 error = image_copyout_memory(fd, sz, ch->ch_u.mem.ptr);
614 image_data(lba_t blk, lba_t size)
620 ch = image_chunk_find(blk);
623 if (ch->ch_type != CH_TYPE_ZEROES)
625 lim = ch->ch_block + (ch->ch_size / secsz);
626 if (lim >= blk + size)
642 image_set_size(lba_t blk)
646 error = image_chunk_skipto(blk);
653 image_write(lba_t blk, void *buf, ssize_t len)
658 if (!is_empty_sector(buf)) {
659 ch = image_chunk_find(blk);
662 /* We may not be able to write to files. */
663 if (ch->ch_type == CH_TYPE_FILE)
665 if (ch->ch_type == CH_TYPE_ZEROES) {
666 ch = image_chunk_memory(ch, blk);
670 assert(ch->ch_type == CH_TYPE_MEMORY);
671 memcpy(ch->ch_u.mem.ptr, buf, secsz);
674 buf = (char *)buf + secsz;
685 while ((ch = STAILQ_FIRST(&image_chunks)) != NULL) {
686 switch (ch->ch_type) {
688 /* We may be closing the same file multiple times. */
689 if (ch->ch_u.file.fd != -1)
690 close(ch->ch_u.file.fd);
693 free(ch->ch_u.mem.ptr);
698 STAILQ_REMOVE_HEAD(&image_chunks, ch_list);
701 if (image_swap_fd != -1)
702 close(image_swap_fd);
703 unlink(image_swap_file);
711 STAILQ_INIT(&image_chunks);
715 image_swap_pgsz = getpagesize();
717 if (atexit(image_cleanup) == -1)
719 if ((tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0')
721 snprintf(image_swap_file, sizeof(image_swap_file), "%s/mkimg-XXXXXX",
723 image_swap_fd = mkstemp(image_swap_file);
724 if (image_swap_fd == -1)