1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (C) 2015 Intel Corporation. 3 * All rights reserved. 4 */ 5 6 #include "spdk/stdinc.h" 7 8 #include "spdk/fd.h" 9 10 #ifdef __linux__ 11 #include <linux/fs.h> 12 #endif 13 14 static uint64_t 15 dev_get_size(int fd) 16 { 17 #if defined(DIOCGMEDIASIZE) /* FreeBSD */ 18 off_t size; 19 20 if (ioctl(fd, DIOCGMEDIASIZE, &size) == 0) { 21 return size; 22 } 23 #elif defined(__linux__) && defined(BLKGETSIZE64) 24 uint64_t size; 25 26 if (ioctl(fd, BLKGETSIZE64, &size) == 0) { 27 return size; 28 } 29 #endif 30 31 return 0; 32 } 33 34 uint32_t 35 spdk_fd_get_blocklen(int fd) 36 { 37 #if defined(DKIOCGETBLOCKSIZE) /* FreeBSD */ 38 uint32_t blocklen; 39 40 if (ioctl(fd, DKIOCGETBLOCKSIZE, &blocklen) == 0) { 41 return blocklen; 42 } 43 #elif defined(__linux__) && defined(BLKSSZGET) 44 uint32_t blocklen; 45 46 if (ioctl(fd, BLKSSZGET, &blocklen) == 0) { 47 return blocklen; 48 } 49 #endif 50 51 return 0; 52 } 53 54 uint64_t 55 spdk_fd_get_size(int fd) 56 { 57 struct stat st; 58 59 if (fstat(fd, &st) != 0) { 60 return 0; 61 } 62 63 if (S_ISLNK(st.st_mode)) { 64 return 0; 65 } 66 67 if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)) { 68 return dev_get_size(fd); 69 } else if (S_ISREG(st.st_mode)) { 70 return st.st_size; 71 } 72 73 /* Not REG, CHR or BLK */ 74 return 0; 75 } 76