getsectsize.c revision c4012e5a7ace2e1f382c4d46f56e5749758c9a1a
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 * Returns the physical sector size of a device
66 */
67errcode_t ext2fs_get_device_phys_sectsize(const char *file, int *sectsize)
68{
69	int	fd;
70
71	fd = ext2fs_open_file(file, O_RDONLY, 0);
72	if (fd < 0)
73		return errno;
74
75#ifdef BLKPBSZGET
76	if (ioctl(fd, BLKPBSZGET, sectsize) >= 0) {
77		close(fd);
78		return 0;
79	}
80#endif
81	*sectsize = 0;
82	close(fd);
83	return 0;
84}
85