summaryrefslogtreecommitdiff
path: root/sysutils/linuxfdisk/files/linuxfdisk-sys_bsd.c
blob: eb3a97ea92265ceb94d2fe37e122b1c4d2a0c95c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <sys/param.h>
#include <sys/disklabel.h>
#if __FreeBSD_version < 500000
#include <sys/diskslice.h>
#include <sys/stat.h>
#else
#include <sys/disk.h>
#include <errno.h>
#endif
#include <stddef.h>
#include "common.h"

unsigned int
sys_bsd_sectorsize(int fd)
{
#ifdef DIOCGSECTORSIZE
	;{
		unsigned int d;
		if (ioctl(fd, DIOCGSECTORSIZE, &d) == 0)
			return d;
	}
#endif
#ifdef DIOCGDINFO
	;{
		struct disklabel dl;
		if (ioctl(fd, DIOCGDINFO, &dl) == 0)
			return dl.d_secsize;
	}
#endif
#ifdef DIOCGSLICEINFO
	;{
		struct diskslices dss;
		if (ioctl(fd, DIOCGSLICEINFO, &dss) == 0)
			return dss.dss_secsize;
	}
#endif
	return 0;
}

int
sys_bsd_getsectors(int fd, unsigned long *s)
{
	/* XXX */
	struct disklabel dl;
#if defined(DIOCGMEDIASIZE) && defined(DIOCGSECTORSIZE)
	;{
		off_t fullsize;
		unsigned sectsize;
		if (ioctl(fd, DIOCGMEDIASIZE, &fullsize) == 0 &&
		    ioctl(fd, DIOCGSECTORSIZE, &sectsize) == 0)
		{
			*s = fullsize / sectsize;
			return 0;
		}
	}
#endif
#ifdef DIOCGSLICEINFO
	/* XXX it is somehow ugly, but seems to work on 4.x. */
	;{
		struct diskslices dss;
		struct stat st;
		if (ioctl(fd, DIOCGSLICEINFO, &dss) == 0 &&
		    fstat(fd, &st) == 0)
		{
			int slice = 31 & (st.st_rdev >> 16);
			/* If it have disklabel, fall to disklabel case,
			 * because it shows more exact info.
			 */
			if (slice != WHOLE_DISK_SLICE &&
			    dss.dss_slices[slice].ds_label != NULL &&
			    ioctl(fd, DIOCGDINFO, &dl) == 0) {
				*s = (unsigned long) dl.d_secperunit;
				return 0;
			}
			*s = dss.dss_slices[slice].ds_size;
			return 0;
		}
	}
#endif
#ifdef DIOCGDINFO
	/* Fallback method. */
	if (ioctl(fd, DIOCGDINFO, &dl) == 0) {
		*s = (unsigned long) dl.d_secperunit;
		return 0;
	}
#endif
	return -1;
}

int
sys_bsd_ptsync(int fd)
{
#ifdef DIOCSYNCSLICEINFO
	return ioctl(fd, DIOCSYNCSLICEINFO, NULL);
#else
	/* XXX I suppose here GEOM systems which:
	 * 1) Don't allow writing to raw disk if it is mounted anywhere,
	 * 2) Automatically update GEOM structures after writing to disk.
	 * Hence, no explicit syncing is required.
	 */
	return 0;
#endif
}

int
sys_bsd_getgeometry(int fd, struct hd_geometry *g)
{
	/* XXX */
	struct disklabel dl;
#ifdef DIOCGDINFO
	if (ioctl(fd, DIOCGDINFO, &dl) < 0)
#endif
		return -1;
	g->cylinders = dl.d_ncylinders;
	g->heads = dl.d_ntracks;
	g->sectors = dl.d_nsectors;
	return 0;
}