2 * getsectsize.c --- get the sector size of a device.
4 * Copyright (C) 1995, 1995 Theodore Ts'o.
5 * Copyright (C) 2003 VMware, Inc.
8 * This file may be redistributed under the terms of the GNU Library
9 * General Public License, version 2.
13 #ifndef _LARGEFILE_SOURCE
14 #define _LARGEFILE_SOURCE
16 #ifndef _LARGEFILE64_SOURCE
17 #define _LARGEFILE64_SOURCE
29 #ifdef HAVE_SYS_DISK_H
32 #ifdef HAVE_LINUX_FD_H
33 #include <sys/ioctl.h>
37 #if defined(__linux__) && defined(_IO)
38 #if !defined(BLKSSZGET)
39 #define BLKSSZGET _IO(0x12,104)/* get block device sector size */
41 #if !defined(BLKPBSZGET)
42 #define BLKPBSZGET _IO(0x12,123)/* get block physical sector size */
50 * Returns the logical sector size of a device
52 errcode_t ext2fs_get_device_sectsize(const char *file, int *sectsize)
56 fd = ext2fs_open_file(file, O_RDONLY, 0);
61 if (ioctl(fd, BLKSSZGET, sectsize) >= 0) {
66 #ifdef DIOCGSECTORSIZE
67 if (ioctl(fd, DIOCGSECTORSIZE, sectsize) >= 0) {
78 * Return desired alignment for direct I/O
80 int ext2fs_get_dio_alignment(int fd)
85 if (ioctl(fd, BLKSSZGET, &align) < 0)
88 #ifdef DIOCGSECTORSIZE
90 ioctl(fd, DIOCGSECTORSIZE, &align) < 0)
96 align = sysconf(_SC_PAGESIZE);
98 #ifdef HAVE_GETPAGESIZE
100 align = getpagesize();
109 * Returns the physical sector size of a device
111 errcode_t ext2fs_get_device_phys_sectsize(const char *file, int *sectsize)
115 fd = ext2fs_open_file(file, O_RDONLY, 0);
120 if (ioctl(fd, BLKPBSZGET, sectsize) >= 0) {
125 #ifdef DIOCGSECTORSIZE
126 /* This isn't really the physical sector size, but FreeBSD
127 * doesn't seem to have this concept. */
128 if (ioctl(fd, DIOCGSECTORSIZE, sectsize) >= 0) {