1*bc3b2c55SMaxim Sobolev /* 2*bc3b2c55SMaxim Sobolev * Copyright (c) 2004-2016 Maxim Sobolev <sobomax@FreeBSD.org> 3*bc3b2c55SMaxim Sobolev * All rights reserved. 4*bc3b2c55SMaxim Sobolev * 5*bc3b2c55SMaxim Sobolev * Redistribution and use in source and binary forms, with or without 6*bc3b2c55SMaxim Sobolev * modification, are permitted provided that the following conditions 7*bc3b2c55SMaxim Sobolev * are met: 8*bc3b2c55SMaxim Sobolev * 1. Redistributions of source code must retain the above copyright 9*bc3b2c55SMaxim Sobolev * notice, this list of conditions and the following disclaimer. 10*bc3b2c55SMaxim Sobolev * 2. Redistributions in binary form must reproduce the above copyright 11*bc3b2c55SMaxim Sobolev * notice, this list of conditions and the following disclaimer in the 12*bc3b2c55SMaxim Sobolev * documentation and/or other materials provided with the distribution. 13*bc3b2c55SMaxim Sobolev * 14*bc3b2c55SMaxim Sobolev * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15*bc3b2c55SMaxim Sobolev * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16*bc3b2c55SMaxim Sobolev * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17*bc3b2c55SMaxim Sobolev * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18*bc3b2c55SMaxim Sobolev * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19*bc3b2c55SMaxim Sobolev * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20*bc3b2c55SMaxim Sobolev * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21*bc3b2c55SMaxim Sobolev * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22*bc3b2c55SMaxim Sobolev * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23*bc3b2c55SMaxim Sobolev * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24*bc3b2c55SMaxim Sobolev * SUCH DAMAGE. 25*bc3b2c55SMaxim Sobolev */ 26*bc3b2c55SMaxim Sobolev 27*bc3b2c55SMaxim Sobolev #include <sys/cdefs.h> 28*bc3b2c55SMaxim Sobolev __FBSDID("$FreeBSD$"); 29*bc3b2c55SMaxim Sobolev 30*bc3b2c55SMaxim Sobolev #include <sys/disk.h> 31*bc3b2c55SMaxim Sobolev #include <sys/ioctl.h> 32*bc3b2c55SMaxim Sobolev #include <sys/param.h> 33*bc3b2c55SMaxim Sobolev #include <sys/mount.h> 34*bc3b2c55SMaxim Sobolev #include <sys/stat.h> 35*bc3b2c55SMaxim Sobolev #include <err.h> 36*bc3b2c55SMaxim Sobolev #include <fcntl.h> 37*bc3b2c55SMaxim Sobolev 38*bc3b2c55SMaxim Sobolev #include "mkuz_cfg.h" 39*bc3b2c55SMaxim Sobolev #include "mkuz_insize.h" 40*bc3b2c55SMaxim Sobolev 41*bc3b2c55SMaxim Sobolev off_t 42*bc3b2c55SMaxim Sobolev mkuz_get_insize(struct mkuz_cfg *cfp) 43*bc3b2c55SMaxim Sobolev { 44*bc3b2c55SMaxim Sobolev int ffd; 45*bc3b2c55SMaxim Sobolev off_t ms; 46*bc3b2c55SMaxim Sobolev struct stat sb; 47*bc3b2c55SMaxim Sobolev struct statfs statfsbuf; 48*bc3b2c55SMaxim Sobolev 49*bc3b2c55SMaxim Sobolev if (fstat(cfp->fdr, &sb) != 0) { 50*bc3b2c55SMaxim Sobolev warn("fstat(%s)", cfp->iname); 51*bc3b2c55SMaxim Sobolev return (-1); 52*bc3b2c55SMaxim Sobolev } 53*bc3b2c55SMaxim Sobolev if ((sb.st_flags & SF_SNAPSHOT) != 0) { 54*bc3b2c55SMaxim Sobolev if (fstatfs(cfp->fdr, &statfsbuf) != 0) { 55*bc3b2c55SMaxim Sobolev warn("fstatfs(%s)", cfp->iname); 56*bc3b2c55SMaxim Sobolev return (-1); 57*bc3b2c55SMaxim Sobolev } 58*bc3b2c55SMaxim Sobolev ffd = open(statfsbuf.f_mntfromname, O_RDONLY); 59*bc3b2c55SMaxim Sobolev if (ffd < 0) { 60*bc3b2c55SMaxim Sobolev warn("open(%s, O_RDONLY)", statfsbuf.f_mntfromname); 61*bc3b2c55SMaxim Sobolev return (-1); 62*bc3b2c55SMaxim Sobolev } 63*bc3b2c55SMaxim Sobolev if (ioctl(ffd, DIOCGMEDIASIZE, &ms) < 0) { 64*bc3b2c55SMaxim Sobolev warn("ioctl(DIOCGMEDIASIZE)"); 65*bc3b2c55SMaxim Sobolev return (-1); 66*bc3b2c55SMaxim Sobolev } 67*bc3b2c55SMaxim Sobolev sb.st_size = ms; 68*bc3b2c55SMaxim Sobolev } else if (S_ISCHR(sb.st_mode)) { 69*bc3b2c55SMaxim Sobolev if (ioctl(cfp->fdr, DIOCGMEDIASIZE, &ms) < 0) { 70*bc3b2c55SMaxim Sobolev warn("ioctl(DIOCGMEDIASIZE)"); 71*bc3b2c55SMaxim Sobolev return (-1); 72*bc3b2c55SMaxim Sobolev } 73*bc3b2c55SMaxim Sobolev sb.st_size = ms; 74*bc3b2c55SMaxim Sobolev } else if (!S_ISREG(sb.st_mode)) { 75*bc3b2c55SMaxim Sobolev warnx("%s: not a character device or regular file\n", 76*bc3b2c55SMaxim Sobolev cfp->iname); 77*bc3b2c55SMaxim Sobolev return (-1); 78*bc3b2c55SMaxim Sobolev } 79*bc3b2c55SMaxim Sobolev return (sb.st_size); 80*bc3b2c55SMaxim Sobolev } 81