Coverage Report

Created: 2023-09-25 06:05

/src/e2fsprogs/lib/ext2fs/getsectsize.c
Line
Count
Source (jump to first uncovered line)
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
0
#define BLKSSZGET  _IO(0x12,104)/* get block device sector size */
40
#endif
41
#if !defined(BLKPBSZGET)
42
0
#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
 */
52
errcode_t ext2fs_get_device_sectsize(const char *file, int *sectsize)
53
0
{
54
#ifdef _WIN32
55
  *sectsize = 512; // just guessing
56
  return 0;
57
#else // not _WIN32
58
59
0
  int fd;
60
61
0
  fd = ext2fs_open_file(file, O_RDONLY, 0);
62
0
  if (fd < 0)
63
0
    return errno;
64
65
0
#ifdef BLKSSZGET
66
0
  if (ioctl(fd, BLKSSZGET, sectsize) >= 0) {
67
0
    close(fd);
68
0
    return 0;
69
0
  }
70
0
#endif
71
#ifdef DIOCGSECTORSIZE
72
  if (ioctl(fd, DIOCGSECTORSIZE, sectsize) >= 0) {
73
    close(fd);
74
    return 0;
75
  }
76
#endif
77
0
  *sectsize = 0;
78
0
  close(fd);
79
0
  return 0;
80
81
0
#endif // ifdef _WIN32
82
0
}
83
84
/*
85
 * Return desired alignment for direct I/O
86
 */
87
int ext2fs_get_dio_alignment(int fd)
88
0
{
89
0
  int align = 0;
90
91
0
#ifdef BLKSSZGET
92
0
  if (ioctl(fd, BLKSSZGET, &align) < 0)
93
0
    align = 0;
94
0
#endif
95
#ifdef DIOCGSECTORSIZE
96
  if (align <= 0 &&
97
      ioctl(fd, DIOCGSECTORSIZE, &align) < 0)
98
    align = 0;
99
#endif
100
101
0
#ifdef _SC_PAGESIZE
102
0
  if (align <= 0)
103
0
    align = sysconf(_SC_PAGESIZE);
104
0
#endif
105
#ifdef HAVE_GETPAGESIZE
106
  if (align <= 0)
107
    align = getpagesize();
108
#endif
109
0
  if (align <= 0)
110
0
    align = 4096;
111
112
0
  return align;
113
0
}
114
115
/*
116
 * Returns the physical sector size of a device
117
 */
118
errcode_t ext2fs_get_device_phys_sectsize(const char *file, int *sectsize)
119
0
{
120
#ifdef _WIN32
121
122
  return ext2fs_get_device_sectsize(file, sectsize);
123
124
#else // not _WIN32
125
126
0
  int fd;
127
128
0
  fd = ext2fs_open_file(file, O_RDONLY, 0);
129
0
  if (fd < 0)
130
0
    return errno;
131
132
0
#ifdef BLKPBSZGET
133
0
  if (ioctl(fd, BLKPBSZGET, sectsize) >= 0) {
134
0
    close(fd);
135
0
    return 0;
136
0
  }
137
0
#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
0
  *sectsize = 0;
147
0
  close(fd);
148
0
  return 0;
149
150
0
#endif // ifdef _WIN32
151
0
}