1 /* 2 * getsectsize.c --- get the sector size of a device. 3 * 4 * Copyright (C) 1995, 1995 Theodore Ts'o. 5 * Copyright (C) 2003 VMware, Inc. 6 * 7 * %Begin-Header% 8 * This file may be redistributed under the terms of the GNU Library 9 * General Public License, version 2. 10 * %End-Header% 11 */ 12 13 #define _LARGEFILE_SOURCE 14 #define _LARGEFILE64_SOURCE 15 16 #include <stdio.h> 17 #if HAVE_UNISTD_H 18 #include <unistd.h> 19 #endif 20 #if HAVE_ERRNO_H 21 #include <errno.h> 22 #endif 23 #include <fcntl.h> 24 #ifdef HAVE_LINUX_FD_H 25 #include <sys/ioctl.h> 26 #include <linux/fd.h> 27 #endif 28 29 #if defined(__linux__) && defined(_IO) 30 #if !defined(BLKSSZGET) 31 #define BLKSSZGET _IO(0x12,104)/* get block device sector size */ 32 #endif 33 #if !defined(BLKPBSZGET) 34 #define BLKPBSZGET _IO(0x12,123)/* get block physical sector size */ 35 #endif 36 #endif 37 38 #include "ext2_fs.h" 39 #include "ext2fs.h" 40 41 /* 42 * Returns the logical sector size of a device 43 */ 44 errcode_t ext2fs_get_device_sectsize(const char *file, int *sectsize) 45 { 46 int fd; 47 48 #ifdef HAVE_OPEN64 49 fd = open64(file, O_RDONLY); 50 #else 51 fd = open(file, O_RDONLY); 52 #endif 53 if (fd < 0) 54 return errno; 55 56 #ifdef BLKSSZGET 57 if (ioctl(fd, BLKSSZGET, sectsize) >= 0) { 58 close(fd); 59 return 0; 60 } 61 #endif 62 *sectsize = 0; 63 close(fd); 64 return 0; 65 } 66 67 /* 68 * Returns the physical sector size of a device 69 */ 70 errcode_t ext2fs_get_device_phys_sectsize(const char *file, int *sectsize) 71 { 72 int fd; 73 74 #ifdef HAVE_OPEN64 75 fd = open64(file, O_RDONLY); 76 #else 77 fd = open(file, O_RDONLY); 78 #endif 79 if (fd < 0) 80 return errno; 81 82 #ifdef BLKPBSZGET 83 if (ioctl(fd, BLKPBSZGET, sectsize) >= 0) { 84 close(fd); 85 return 0; 86 } 87 #endif 88 *sectsize = 0; 89 close(fd); 90 return 0; 91 } 92