getsectsize.c revision dd0a2679ddd0a9bf53e32efc0f67a7e7a5ea5f00
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 "config.h" 17#include <stdio.h> 18#if HAVE_UNISTD_H 19#include <unistd.h> 20#endif 21#if HAVE_ERRNO_H 22#include <errno.h> 23#endif 24#include <fcntl.h> 25#ifdef HAVE_LINUX_FD_H 26#include <sys/ioctl.h> 27#include <linux/fd.h> 28#endif 29 30#if defined(__linux__) && defined(_IO) 31#if !defined(BLKSSZGET) 32#define BLKSSZGET _IO(0x12,104)/* get block device sector size */ 33#endif 34#if !defined(BLKPBSZGET) 35#define BLKPBSZGET _IO(0x12,123)/* get block physical sector size */ 36#endif 37#endif 38 39#include "ext2_fs.h" 40#include "ext2fs.h" 41 42/* 43 * Returns the logical sector size of a device 44 */ 45errcode_t ext2fs_get_device_sectsize(const char *file, int *sectsize) 46{ 47 int fd; 48 49 fd = ext2fs_open_file(file, O_RDONLY, 0); 50 if (fd < 0) 51 return errno; 52 53#ifdef BLKSSZGET 54 if (ioctl(fd, BLKSSZGET, sectsize) >= 0) { 55 close(fd); 56 return 0; 57 } 58#endif 59 *sectsize = 0; 60 close(fd); 61 return 0; 62} 63 64/* 65 * Return desired alignment for direct I/O 66 */ 67int ext2fs_get_dio_alignment(int fd) 68{ 69 int align = 0; 70 71#ifdef BLKSSZGET 72 if (ioctl(fd, BLKSSZGET, &align) < 0) 73 align = 0; 74#endif 75 76#ifdef _SC_PAGESIZE 77 if (align <= 0) 78 align = sysconf(_SC_PAGESIZE); 79#endif 80#ifdef HAVE_GETPAGESIZE 81 if (align <= 0) 82 align = getpagesize(); 83#endif 84 if (align <= 0) 85 align = 4096; 86 87 return align; 88} 89 90/* 91 * Returns the physical sector size of a device 92 */ 93errcode_t ext2fs_get_device_phys_sectsize(const char *file, int *sectsize) 94{ 95 int fd; 96 97 fd = ext2fs_open_file(file, O_RDONLY, 0); 98 if (fd < 0) 99 return errno; 100 101#ifdef BLKPBSZGET 102 if (ioctl(fd, BLKPBSZGET, sectsize) >= 0) { 103 close(fd); 104 return 0; 105 } 106#endif 107 *sectsize = 0; 108 close(fd); 109 return 0; 110} 111