1 /*- 2 * Copyright (c) 2010 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Pawel Jakub Dawidek under sponsorship from 6 * the FreeBSD Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/types.h> 34 #include <sys/disk.h> 35 #include <sys/ioctl.h> 36 #include <sys/stat.h> 37 38 #include <assert.h> 39 #include <errno.h> 40 #include <fcntl.h> 41 42 #include <pjdlog.h> 43 44 #include "hast.h" 45 #include "subr.h" 46 47 int 48 provinfo(struct hast_resource *res, bool dowrite) 49 { 50 struct stat sb; 51 52 assert(res->hr_localpath != NULL && res->hr_localpath[0] != '\0'); 53 54 if (res->hr_localfd == -1) { 55 res->hr_localfd = open(res->hr_localpath, 56 dowrite ? O_RDWR : O_RDONLY); 57 if (res->hr_localfd < 0) { 58 KEEP_ERRNO(pjdlog_errno(LOG_ERR, "Unable to open %s", 59 res->hr_localpath)); 60 return (-1); 61 } 62 } 63 if (fstat(res->hr_localfd, &sb) < 0) { 64 KEEP_ERRNO(pjdlog_errno(LOG_ERR, "Unable to stat %s", 65 res->hr_localpath)); 66 return (-1); 67 } 68 if (S_ISCHR(sb.st_mode)) { 69 /* 70 * If this is character device, it is most likely GEOM provider. 71 */ 72 if (ioctl(res->hr_localfd, DIOCGMEDIASIZE, 73 &res->hr_local_mediasize) < 0) { 74 KEEP_ERRNO(pjdlog_errno(LOG_ERR, 75 "Unable obtain provider %s mediasize", 76 res->hr_localpath)); 77 return (-1); 78 } 79 if (ioctl(res->hr_localfd, DIOCGSECTORSIZE, 80 &res->hr_local_sectorsize) < 0) { 81 KEEP_ERRNO(pjdlog_errno(LOG_ERR, 82 "Unable obtain provider %s sectorsize", 83 res->hr_localpath)); 84 return (-1); 85 } 86 } else if (S_ISREG(sb.st_mode)) { 87 /* 88 * We also support regular files for which we hardcode 89 * sector size of 512 bytes. 90 */ 91 res->hr_local_mediasize = sb.st_size; 92 res->hr_local_sectorsize = 512; 93 } else { 94 /* 95 * We support no other file types. 96 */ 97 pjdlog_error("%s is neither GEOM provider nor regular file.", 98 res->hr_localpath); 99 errno = EFTYPE; 100 return (-1); 101 } 102 return (0); 103 } 104 105 const char * 106 role2str(int role) 107 { 108 109 switch (role) { 110 case HAST_ROLE_INIT: 111 return ("init"); 112 case HAST_ROLE_PRIMARY: 113 return ("primary"); 114 case HAST_ROLE_SECONDARY: 115 return ("secondary"); 116 } 117 return ("unknown"); 118 } 119