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 #ifndef _LARGEFILE_SOURCE
14 #define _LARGEFILE_SOURCE
15 #endif
16 #ifndef _LARGEFILE64_SOURCE
17 #define _LARGEFILE64_SOURCE
18 #endif
19
20 #include "config.h"
21 #include <stdio.h>
22 #if HAVE_UNISTD_H
23 #include <unistd.h>
24 #endif
25 #if HAVE_ERRNO_H
26 #include <errno.h>
27 #endif
28 #include <fcntl.h>
29 #ifdef HAVE_SYS_DISK_H
30 #include <sys/disk.h>
31 #endif
32 #ifdef HAVE_LINUX_FD_H
33 #include <sys/ioctl.h>
34 #include <linux/fd.h>
35 #endif
36
37 #if defined(__linux__) && defined(_IO)
38 #if !defined(BLKSSZGET)
39 #define BLKSSZGET _IO(0x12,104)/* get block device sector size */
40 #endif
41 #if !defined(BLKPBSZGET)
42 #define BLKPBSZGET _IO(0x12,123)/* get block physical sector size */
43 #endif
44 #endif
45
46 #include "ext2_fs.h"
47 #include "ext2fs.h"
48
49 /*
50 * Returns the logical sector size of a device
51 */
ext2fs_get_device_sectsize(const char * file,int * sectsize)52 errcode_t ext2fs_get_device_sectsize(const char *file, int *sectsize)
53 {
54 #ifdef _WIN64
55 *sectsize = 512; // just guessing
56 return 0;
57 #else // not _WIN64
58
59 int fd;
60
61 fd = ext2fs_open_file(file, O_RDONLY, 0);
62 if (fd < 0)
63 return errno;
64
65 #ifdef BLKSSZGET
66 if (ioctl(fd, BLKSSZGET, sectsize) >= 0) {
67 close(fd);
68 return 0;
69 }
70 #endif
71 #ifdef DIOCGSECTORSIZE
72 if (ioctl(fd, DIOCGSECTORSIZE, sectsize) >= 0) {
73 close(fd);
74 return 0;
75 }
76 #endif
77 *sectsize = 0;
78 close(fd);
79 return 0;
80
81 #endif // ifdef _WIN64
82 }
83
84 /*
85 * Return desired alignment for direct I/O
86 */
ext2fs_get_dio_alignment(int fd)87 int ext2fs_get_dio_alignment(int fd)
88 {
89 int align = 0;
90
91 #ifdef BLKSSZGET
92 if (ioctl(fd, BLKSSZGET, &align) < 0)
93 align = 0;
94 #endif
95 #ifdef DIOCGSECTORSIZE
96 if (align <= 0 &&
97 ioctl(fd, DIOCGSECTORSIZE, &align) < 0)
98 align = 0;
99 #endif
100
101 #ifdef _SC_PAGESIZE
102 if (align <= 0)
103 align = sysconf(_SC_PAGESIZE);
104 #endif
105 #ifdef HAVE_GETPAGESIZE
106 if (align <= 0)
107 align = getpagesize();
108 #endif
109 if (align <= 0)
110 align = 4096;
111
112 return align;
113 }
114
115 /*
116 * Returns the physical sector size of a device
117 */
ext2fs_get_device_phys_sectsize(const char * file,int * sectsize)118 errcode_t ext2fs_get_device_phys_sectsize(const char *file, int *sectsize)
119 {
120 #ifdef _WIN64
121
122 return ext2fs_get_device_sectsize(file, sectsize);
123
124 #else // not _WIN64
125
126 int fd;
127
128 fd = ext2fs_open_file(file, O_RDONLY, 0);
129 if (fd < 0)
130 return errno;
131
132 #ifdef BLKPBSZGET
133 if (ioctl(fd, BLKPBSZGET, sectsize) >= 0) {
134 close(fd);
135 return 0;
136 }
137 #endif
138 #ifdef DIOCGSECTORSIZE
139 /* This isn't really the physical sector size, but FreeBSD
140 * doesn't seem to have this concept. */
141 if (ioctl(fd, DIOCGSECTORSIZE, sectsize) >= 0) {
142 close(fd);
143 return 0;
144 }
145 #endif
146 *sectsize = 0;
147 close(fd);
148 return 0;
149
150 #endif // ifdef _WIN64
151 }
152