1*1b191cb5SApple OSS Distributions #ifdef T_NAMESPACE
2*1b191cb5SApple OSS Distributions #undef T_NAMESPACE
3*1b191cb5SApple OSS Distributions #endif
4*1b191cb5SApple OSS Distributions #include <darwintest.h>
5*1b191cb5SApple OSS Distributions #include <darwintest_utils.h>
6*1b191cb5SApple OSS Distributions
7*1b191cb5SApple OSS Distributions #include <stdlib.h>
8*1b191cb5SApple OSS Distributions #include <unistd.h>
9*1b191cb5SApple OSS Distributions #include <fcntl.h>
10*1b191cb5SApple OSS Distributions #include <System/sys/fsctl.h>
11*1b191cb5SApple OSS Distributions #include <paths.h>
12*1b191cb5SApple OSS Distributions
13*1b191cb5SApple OSS Distributions static char *mktempdir(void);
14*1b191cb5SApple OSS Distributions static char *mktempmount(void);
15*1b191cb5SApple OSS Distributions
16*1b191cb5SApple OSS Distributions #ifndef TEST_UNENTITLED
17*1b191cb5SApple OSS Distributions static int system_legal(const char *command);
18*1b191cb5SApple OSS Distributions static char *mkramdisk(void);
19*1b191cb5SApple OSS Distributions static uint64_t time_for_read(int fd, const char *expected);
20*1b191cb5SApple OSS Distributions static void perf_setup(char **path, int *fd);
21*1b191cb5SApple OSS Distributions
22*1b191cb5SApple OSS Distributions #define READSIZE 1024L
23*1b191cb5SApple OSS Distributions #endif /* !TEST_UNENTITLED */
24*1b191cb5SApple OSS Distributions
25*1b191cb5SApple OSS Distributions T_GLOBAL_META(
26*1b191cb5SApple OSS Distributions T_META_NAMESPACE("xnu.vfs.dmc"),
27*1b191cb5SApple OSS Distributions T_META_ASROOT(true),
28*1b191cb5SApple OSS Distributions T_META_RUN_CONCURRENTLY(true)
29*1b191cb5SApple OSS Distributions );
30*1b191cb5SApple OSS Distributions
31*1b191cb5SApple OSS Distributions #pragma mark Entitled Tests
32*1b191cb5SApple OSS Distributions
33*1b191cb5SApple OSS Distributions #ifndef TEST_UNENTITLED
34*1b191cb5SApple OSS Distributions T_DECL(fsctl_get_uninitialized,
35*1b191cb5SApple OSS Distributions "Initial fsctl.get should return zeros",
36*1b191cb5SApple OSS Distributions T_META_ASROOT(false))
37*1b191cb5SApple OSS Distributions {
38*1b191cb5SApple OSS Distributions int err;
39*1b191cb5SApple OSS Distributions char *mount_path;
40*1b191cb5SApple OSS Distributions disk_conditioner_info info = {0};
41*1b191cb5SApple OSS Distributions disk_conditioner_info expected_info = {0};
42*1b191cb5SApple OSS Distributions
43*1b191cb5SApple OSS Distributions T_SETUPBEGIN;
44*1b191cb5SApple OSS Distributions mount_path = mktempmount();
45*1b191cb5SApple OSS Distributions T_SETUPEND;
46*1b191cb5SApple OSS Distributions
47*1b191cb5SApple OSS Distributions info.enabled = true;
48*1b191cb5SApple OSS Distributions info.is_ssd = true;
49*1b191cb5SApple OSS Distributions err = fsctl(mount_path, DISK_CONDITIONER_IOC_GET, &info, 0);
50*1b191cb5SApple OSS Distributions T_WITH_ERRNO;
51*1b191cb5SApple OSS Distributions T_ASSERT_EQ_INT(0, err, "fsctl(DISK_CONDITIONER_IOC_GET)");
52*1b191cb5SApple OSS Distributions
53*1b191cb5SApple OSS Distributions err = memcmp(&info, &expected_info, sizeof(info));
54*1b191cb5SApple OSS Distributions T_ASSERT_EQ_INT(0, err, "initial DMC info is zeroed");
55*1b191cb5SApple OSS Distributions }
56*1b191cb5SApple OSS Distributions
57*1b191cb5SApple OSS Distributions T_DECL(fsctl_set,
58*1b191cb5SApple OSS Distributions "fsctl.set should succeed and fsctl.get should verify")
59*1b191cb5SApple OSS Distributions {
60*1b191cb5SApple OSS Distributions int err;
61*1b191cb5SApple OSS Distributions char *mount_path;
62*1b191cb5SApple OSS Distributions disk_conditioner_info info = {0};
63*1b191cb5SApple OSS Distributions disk_conditioner_info expected_info = {0};
64*1b191cb5SApple OSS Distributions
65*1b191cb5SApple OSS Distributions T_SETUPBEGIN;
66*1b191cb5SApple OSS Distributions mount_path = mktempmount();
67*1b191cb5SApple OSS Distributions T_SETUPEND;
68*1b191cb5SApple OSS Distributions
69*1b191cb5SApple OSS Distributions info.enabled = 1;
70*1b191cb5SApple OSS Distributions info.access_time_usec = 10;
71*1b191cb5SApple OSS Distributions info.read_throughput_mbps = 40;
72*1b191cb5SApple OSS Distributions info.write_throughput_mbps = 40;
73*1b191cb5SApple OSS Distributions info.is_ssd = 0;
74*1b191cb5SApple OSS Distributions info.ioqueue_depth = 8;
75*1b191cb5SApple OSS Distributions info.maxreadcnt = 8;
76*1b191cb5SApple OSS Distributions info.maxwritecnt = 8;
77*1b191cb5SApple OSS Distributions info.segreadcnt = 8;
78*1b191cb5SApple OSS Distributions info.segwritecnt = 8;
79*1b191cb5SApple OSS Distributions expected_info = info;
80*1b191cb5SApple OSS Distributions
81*1b191cb5SApple OSS Distributions err = fsctl(mount_path, DISK_CONDITIONER_IOC_SET, &info, 0);
82*1b191cb5SApple OSS Distributions T_WITH_ERRNO;
83*1b191cb5SApple OSS Distributions T_ASSERT_EQ_INT(0, err, "fsctl(DISK_CONDITIONER_IOC_SET)");
84*1b191cb5SApple OSS Distributions
85*1b191cb5SApple OSS Distributions err = fsctl(mount_path, DISK_CONDITIONER_IOC_GET, &info, 0);
86*1b191cb5SApple OSS Distributions T_WITH_ERRNO;
87*1b191cb5SApple OSS Distributions T_ASSERT_EQ_INT(0, err, "fsctl(DISK_CONDITIONER_IOC_GET) after SET");
88*1b191cb5SApple OSS Distributions
89*1b191cb5SApple OSS Distributions err = memcmp(&info, &expected_info, sizeof(info));
90*1b191cb5SApple OSS Distributions T_ASSERT_EQ_INT(0, err, "fsctl.get is the info configured by fsctl.set");
91*1b191cb5SApple OSS Distributions }
92*1b191cb5SApple OSS Distributions
93*1b191cb5SApple OSS Distributions static void
verify_mount_fallback_values(const char * mount_path,disk_conditioner_info * info)94*1b191cb5SApple OSS Distributions verify_mount_fallback_values(const char *mount_path, disk_conditioner_info *info)
95*1b191cb5SApple OSS Distributions {
96*1b191cb5SApple OSS Distributions int err;
97*1b191cb5SApple OSS Distributions disk_conditioner_info newinfo = {0};
98*1b191cb5SApple OSS Distributions
99*1b191cb5SApple OSS Distributions err = fsctl(mount_path, DISK_CONDITIONER_IOC_SET, info, 0);
100*1b191cb5SApple OSS Distributions T_WITH_ERRNO;
101*1b191cb5SApple OSS Distributions T_ASSERT_EQ_INT(0, err, "fsctl(DISK_CONDITIONER_IOC_SET)");
102*1b191cb5SApple OSS Distributions
103*1b191cb5SApple OSS Distributions err = fsctl(mount_path, DISK_CONDITIONER_IOC_GET, &newinfo, 0);
104*1b191cb5SApple OSS Distributions T_WITH_ERRNO;
105*1b191cb5SApple OSS Distributions T_ASSERT_EQ_INT(0, err, "fsctl(DISK_CONDITIONER_IOC_GET) after SET");
106*1b191cb5SApple OSS Distributions
107*1b191cb5SApple OSS Distributions // without querying the drive for the expected values, the best we can do is
108*1b191cb5SApple OSS Distributions // assert that they are not zero (impossible) or less than UINT32_MAX (unlikely)
109*1b191cb5SApple OSS Distributions T_ASSERT_GT(newinfo.ioqueue_depth, 0u, "ioqueue_depth is the value from the mount");
110*1b191cb5SApple OSS Distributions T_ASSERT_GT(newinfo.maxreadcnt, 0u, "maxreadcnt is value from the mount");
111*1b191cb5SApple OSS Distributions T_ASSERT_GT(newinfo.maxwritecnt, 0u, "maxwritecnt is value from the mount");
112*1b191cb5SApple OSS Distributions T_ASSERT_GT(newinfo.segreadcnt, 0u, "segreadcnt is value from the mount");
113*1b191cb5SApple OSS Distributions T_ASSERT_GT(newinfo.segwritecnt, 0u, "segwritecnt is value from the mount");
114*1b191cb5SApple OSS Distributions T_ASSERT_LT(newinfo.ioqueue_depth, UINT32_MAX, "ioqueue_depth is the value from the mount");
115*1b191cb5SApple OSS Distributions T_ASSERT_LT(newinfo.maxreadcnt, UINT32_MAX, "maxreadcnt is value from the mount");
116*1b191cb5SApple OSS Distributions T_ASSERT_LT(newinfo.maxwritecnt, UINT32_MAX, "maxwritecnt is value from the mount");
117*1b191cb5SApple OSS Distributions T_ASSERT_LT(newinfo.segreadcnt, UINT32_MAX, "segreadcnt is value from the mount");
118*1b191cb5SApple OSS Distributions T_ASSERT_LT(newinfo.segwritecnt, UINT32_MAX, "segwritecnt is value from the mount");
119*1b191cb5SApple OSS Distributions }
120*1b191cb5SApple OSS Distributions
121*1b191cb5SApple OSS Distributions T_DECL(fsctl_set_zero,
122*1b191cb5SApple OSS Distributions "fsctl.set zero values should fall back to original mount settings")
123*1b191cb5SApple OSS Distributions {
124*1b191cb5SApple OSS Distributions char *mount_path;
125*1b191cb5SApple OSS Distributions disk_conditioner_info info = {0};
126*1b191cb5SApple OSS Distributions
127*1b191cb5SApple OSS Distributions T_SETUPBEGIN;
128*1b191cb5SApple OSS Distributions mount_path = mktempmount();
129*1b191cb5SApple OSS Distributions
130*1b191cb5SApple OSS Distributions info.enabled = 1;
131*1b191cb5SApple OSS Distributions /* everything else is 0 */
132*1b191cb5SApple OSS Distributions
133*1b191cb5SApple OSS Distributions T_SETUPEND;
134*1b191cb5SApple OSS Distributions
135*1b191cb5SApple OSS Distributions verify_mount_fallback_values(mount_path, &info);
136*1b191cb5SApple OSS Distributions }
137*1b191cb5SApple OSS Distributions
138*1b191cb5SApple OSS Distributions T_DECL(fsctl_set_out_of_bounds,
139*1b191cb5SApple OSS Distributions "fsctl.set out-of-bounds values should fall back to original mount settings")
140*1b191cb5SApple OSS Distributions {
141*1b191cb5SApple OSS Distributions char *mount_path;
142*1b191cb5SApple OSS Distributions disk_conditioner_info info;
143*1b191cb5SApple OSS Distributions
144*1b191cb5SApple OSS Distributions T_SETUPBEGIN;
145*1b191cb5SApple OSS Distributions mount_path = mktempmount();
146*1b191cb5SApple OSS Distributions
147*1b191cb5SApple OSS Distributions memset(&info, UINT32_MAX, sizeof(info));
148*1b191cb5SApple OSS Distributions info.enabled = 1;
149*1b191cb5SApple OSS Distributions info.access_time_usec = 0;
150*1b191cb5SApple OSS Distributions info.read_throughput_mbps = 0;
151*1b191cb5SApple OSS Distributions info.write_throughput_mbps = 0;
152*1b191cb5SApple OSS Distributions /* everything else is UINT32_MAX */
153*1b191cb5SApple OSS Distributions
154*1b191cb5SApple OSS Distributions T_SETUPEND;
155*1b191cb5SApple OSS Distributions
156*1b191cb5SApple OSS Distributions verify_mount_fallback_values(mount_path, &info);
157*1b191cb5SApple OSS Distributions }
158*1b191cb5SApple OSS Distributions
159*1b191cb5SApple OSS Distributions T_DECL(fsctl_restore_mount_fields,
160*1b191cb5SApple OSS Distributions "fsctl.set should restore fields on mount_t that it temporarily overrides")
161*1b191cb5SApple OSS Distributions {
162*1b191cb5SApple OSS Distributions int err;
163*1b191cb5SApple OSS Distributions char *mount_path;
164*1b191cb5SApple OSS Distributions disk_conditioner_info info;
165*1b191cb5SApple OSS Distributions disk_conditioner_info mount_fields;
166*1b191cb5SApple OSS Distributions
167*1b191cb5SApple OSS Distributions T_SETUPBEGIN;
168*1b191cb5SApple OSS Distributions mount_path = mktempmount();
169*1b191cb5SApple OSS Distributions T_SETUPEND;
170*1b191cb5SApple OSS Distributions
171*1b191cb5SApple OSS Distributions /* first set out-of-bounds values to retrieve the original mount_t fields */
172*1b191cb5SApple OSS Distributions memset(&info, UINT32_MAX, sizeof(info));
173*1b191cb5SApple OSS Distributions info.enabled = 1;
174*1b191cb5SApple OSS Distributions info.access_time_usec = 0;
175*1b191cb5SApple OSS Distributions info.read_throughput_mbps = 0;
176*1b191cb5SApple OSS Distributions info.write_throughput_mbps = 0;
177*1b191cb5SApple OSS Distributions /* everything else is UINT32_MAX */
178*1b191cb5SApple OSS Distributions err = fsctl(mount_path, DISK_CONDITIONER_IOC_SET, &info, 0);
179*1b191cb5SApple OSS Distributions T_WITH_ERRNO;
180*1b191cb5SApple OSS Distributions T_ASSERT_EQ_INT(0, err, "fsctl(DISK_CONDITIONER_IOC_SET)");
181*1b191cb5SApple OSS Distributions
182*1b191cb5SApple OSS Distributions err = fsctl(mount_path, DISK_CONDITIONER_IOC_GET, &mount_fields, 0);
183*1b191cb5SApple OSS Distributions T_WITH_ERRNO;
184*1b191cb5SApple OSS Distributions T_ASSERT_EQ_INT(0, err, "fsctl(DISK_CONDITIONER_IOC_GET)");
185*1b191cb5SApple OSS Distributions
186*1b191cb5SApple OSS Distributions /* now turn off the disk conditioner which should restore fields on the mount_t */
187*1b191cb5SApple OSS Distributions memset(&info, 1, sizeof(info));
188*1b191cb5SApple OSS Distributions info.enabled = 0;
189*1b191cb5SApple OSS Distributions err = fsctl(mount_path, DISK_CONDITIONER_IOC_SET, &info, 0);
190*1b191cb5SApple OSS Distributions T_WITH_ERRNO;
191*1b191cb5SApple OSS Distributions T_ASSERT_EQ_INT(0, err, "fsctl(DISK_CONDITIONER_IOC_SET)");
192*1b191cb5SApple OSS Distributions
193*1b191cb5SApple OSS Distributions /* and finally set out-of-bounds values again to retrieve the new mount_t fields which should not have changed */
194*1b191cb5SApple OSS Distributions memset(&info, UINT32_MAX, sizeof(info));
195*1b191cb5SApple OSS Distributions info.enabled = 0;
196*1b191cb5SApple OSS Distributions info.access_time_usec = 0;
197*1b191cb5SApple OSS Distributions info.read_throughput_mbps = 0;
198*1b191cb5SApple OSS Distributions info.write_throughput_mbps = 0;
199*1b191cb5SApple OSS Distributions /* everything else is UINT32_MAX */
200*1b191cb5SApple OSS Distributions err = fsctl(mount_path, DISK_CONDITIONER_IOC_SET, &info, 0);
201*1b191cb5SApple OSS Distributions T_WITH_ERRNO;
202*1b191cb5SApple OSS Distributions T_ASSERT_EQ_INT(0, err, "fsctl(DISK_CONDITIONER_IOC_SET)");
203*1b191cb5SApple OSS Distributions
204*1b191cb5SApple OSS Distributions err = fsctl(mount_path, DISK_CONDITIONER_IOC_GET, &info, 0);
205*1b191cb5SApple OSS Distributions T_WITH_ERRNO;
206*1b191cb5SApple OSS Distributions T_ASSERT_EQ_INT(0, err, "fsctl(DISK_CONDITIONER_IOC_GET)");
207*1b191cb5SApple OSS Distributions
208*1b191cb5SApple OSS Distributions T_ASSERT_EQ(info.maxreadcnt, mount_fields.maxreadcnt, "mount_t maxreadcnt restored");
209*1b191cb5SApple OSS Distributions T_ASSERT_EQ(info.maxwritecnt, mount_fields.maxwritecnt, "mount_t maxwritecnt restored");
210*1b191cb5SApple OSS Distributions T_ASSERT_EQ(info.segreadcnt, mount_fields.segreadcnt, "mount_t segreadcnt restored");
211*1b191cb5SApple OSS Distributions T_ASSERT_EQ(info.segwritecnt, mount_fields.segwritecnt, "mount_t segwritecnt restored");
212*1b191cb5SApple OSS Distributions T_ASSERT_EQ(info.ioqueue_depth, mount_fields.ioqueue_depth, "mount_t ioqueue_depth restored");
213*1b191cb5SApple OSS Distributions }
214*1b191cb5SApple OSS Distributions
215*1b191cb5SApple OSS Distributions T_DECL(fsctl_get_nonroot,
216*1b191cb5SApple OSS Distributions "fsctl.get should not require root",
217*1b191cb5SApple OSS Distributions T_META_ASROOT(false))
218*1b191cb5SApple OSS Distributions {
219*1b191cb5SApple OSS Distributions int err;
220*1b191cb5SApple OSS Distributions char *mount_path;
221*1b191cb5SApple OSS Distributions disk_conditioner_info info;
222*1b191cb5SApple OSS Distributions
223*1b191cb5SApple OSS Distributions T_SETUPBEGIN;
224*1b191cb5SApple OSS Distributions // make sure we're not root
225*1b191cb5SApple OSS Distributions if (0 == geteuid()) {
226*1b191cb5SApple OSS Distributions seteuid(5000);
227*1b191cb5SApple OSS Distributions }
228*1b191cb5SApple OSS Distributions
229*1b191cb5SApple OSS Distributions mount_path = mktempmount();
230*1b191cb5SApple OSS Distributions T_SETUPEND;
231*1b191cb5SApple OSS Distributions
232*1b191cb5SApple OSS Distributions err = fsctl(mount_path, DISK_CONDITIONER_IOC_GET, &info, 0);
233*1b191cb5SApple OSS Distributions T_WITH_ERRNO;
234*1b191cb5SApple OSS Distributions T_ASSERT_EQ_INT(0, err, "fsctl.get without root");
235*1b191cb5SApple OSS Distributions }
236*1b191cb5SApple OSS Distributions
237*1b191cb5SApple OSS Distributions T_DECL(fsctl_set_nonroot,
238*1b191cb5SApple OSS Distributions "fsctl.set should require root",
239*1b191cb5SApple OSS Distributions T_META_ASROOT(false))
240*1b191cb5SApple OSS Distributions {
241*1b191cb5SApple OSS Distributions int err;
242*1b191cb5SApple OSS Distributions char *mount_path;
243*1b191cb5SApple OSS Distributions disk_conditioner_info info = {0};
244*1b191cb5SApple OSS Distributions disk_conditioner_info expected_info = {0};
245*1b191cb5SApple OSS Distributions
246*1b191cb5SApple OSS Distributions T_SETUPBEGIN;
247*1b191cb5SApple OSS Distributions // make sure we're not root
248*1b191cb5SApple OSS Distributions if (0 == geteuid()) {
249*1b191cb5SApple OSS Distributions seteuid(5000);
250*1b191cb5SApple OSS Distributions }
251*1b191cb5SApple OSS Distributions
252*1b191cb5SApple OSS Distributions mount_path = mktempmount();
253*1b191cb5SApple OSS Distributions T_SETUPEND;
254*1b191cb5SApple OSS Distributions
255*1b191cb5SApple OSS Distributions // save original info
256*1b191cb5SApple OSS Distributions err = fsctl(mount_path, DISK_CONDITIONER_IOC_GET, &expected_info, 0);
257*1b191cb5SApple OSS Distributions T_WITH_ERRNO;
258*1b191cb5SApple OSS Distributions T_ASSERT_EQ_INT(0, err, "Get original DMC info");
259*1b191cb5SApple OSS Distributions
260*1b191cb5SApple OSS Distributions info.enabled = 1;
261*1b191cb5SApple OSS Distributions info.access_time_usec = 10;
262*1b191cb5SApple OSS Distributions err = fsctl(mount_path, DISK_CONDITIONER_IOC_SET, &info, 0);
263*1b191cb5SApple OSS Distributions T_WITH_ERRNO;
264*1b191cb5SApple OSS Distributions T_ASSERT_NE_INT(0, err, "fsctl.set returns error without root");
265*1b191cb5SApple OSS Distributions
266*1b191cb5SApple OSS Distributions err = fsctl(mount_path, DISK_CONDITIONER_IOC_GET, &info, 0);
267*1b191cb5SApple OSS Distributions T_WITH_ERRNO;
268*1b191cb5SApple OSS Distributions T_ASSERT_EQ_INT(0, err, "fsctl.get after nonroot fsctl.set");
269*1b191cb5SApple OSS Distributions
270*1b191cb5SApple OSS Distributions err = memcmp(&info, &expected_info, sizeof(info));
271*1b191cb5SApple OSS Distributions T_ASSERT_EQ_INT(0, err, "fsctl.set should not change info without root");
272*1b191cb5SApple OSS Distributions }
273*1b191cb5SApple OSS Distributions
274*1b191cb5SApple OSS Distributions T_DECL(fsctl_delays,
275*1b191cb5SApple OSS Distributions "Validate I/O delays when DMC is enabled",
276*1b191cb5SApple OSS Distributions T_META_RUN_CONCURRENTLY(false))
277*1b191cb5SApple OSS Distributions {
278*1b191cb5SApple OSS Distributions char *path;
279*1b191cb5SApple OSS Distributions int fd;
280*1b191cb5SApple OSS Distributions int err;
281*1b191cb5SApple OSS Distributions uint64_t elapsed_nsec, expected_nsec;
282*1b191cb5SApple OSS Distributions disk_conditioner_info info = {0};
283*1b191cb5SApple OSS Distributions char buf[READSIZE];
284*1b191cb5SApple OSS Distributions
285*1b191cb5SApple OSS Distributions T_SETUPBEGIN;
286*1b191cb5SApple OSS Distributions perf_setup(&path, &fd);
287*1b191cb5SApple OSS Distributions memset(buf, 0xFF, sizeof(buf));
288*1b191cb5SApple OSS Distributions T_ASSERT_EQ_LONG((long)sizeof(buf), write(fd, buf, sizeof(buf)), "write random data to temp file");
289*1b191cb5SApple OSS Distributions fcntl(fd, F_FULLFSYNC);
290*1b191cb5SApple OSS Distributions T_SETUPEND;
291*1b191cb5SApple OSS Distributions
292*1b191cb5SApple OSS Distributions expected_nsec = NSEC_PER_SEC / 2;
293*1b191cb5SApple OSS Distributions
294*1b191cb5SApple OSS Distributions // measure delay before setting parameters (should be none)
295*1b191cb5SApple OSS Distributions elapsed_nsec = time_for_read(fd, buf);
296*1b191cb5SApple OSS Distributions T_ASSERT_LT_ULLONG(elapsed_nsec, expected_nsec, "DMC disabled read(%ld) from %s is reasonably fast", READSIZE, path);
297*1b191cb5SApple OSS Distributions
298*1b191cb5SApple OSS Distributions // measure delay after setting parameters
299*1b191cb5SApple OSS Distributions info.enabled = 1;
300*1b191cb5SApple OSS Distributions info.access_time_usec = expected_nsec / NSEC_PER_USEC;
301*1b191cb5SApple OSS Distributions info.read_throughput_mbps = 40;
302*1b191cb5SApple OSS Distributions info.write_throughput_mbps = 40;
303*1b191cb5SApple OSS Distributions info.is_ssd = 1; // is_ssd will ensure we get constant access_time delays rather than scaled
304*1b191cb5SApple OSS Distributions err = fsctl(path, DISK_CONDITIONER_IOC_SET, &info, 0);
305*1b191cb5SApple OSS Distributions T_WITH_ERRNO;
306*1b191cb5SApple OSS Distributions T_ASSERT_EQ_INT(0, err, "fsctl(DISK_CONDITIONER_IOC_SET) delay");
307*1b191cb5SApple OSS Distributions
308*1b191cb5SApple OSS Distributions elapsed_nsec = time_for_read(fd, buf);
309*1b191cb5SApple OSS Distributions T_ASSERT_GT_ULLONG(elapsed_nsec, expected_nsec, "DMC enabled read(%ld) from %s is at least the expected delay", READSIZE, path);
310*1b191cb5SApple OSS Distributions T_ASSERT_LT_ULLONG(elapsed_nsec, 2 * expected_nsec, "DMC enabled read(%ld) from %s is no more than twice the expected delay", READSIZE, path);
311*1b191cb5SApple OSS Distributions
312*1b191cb5SApple OSS Distributions // measure delay after resetting parameters (should be none)
313*1b191cb5SApple OSS Distributions info.enabled = 0;
314*1b191cb5SApple OSS Distributions err = fsctl(path, DISK_CONDITIONER_IOC_SET, &info, 0);
315*1b191cb5SApple OSS Distributions T_WITH_ERRNO;
316*1b191cb5SApple OSS Distributions T_ASSERT_EQ_INT(0, err, "fsctl(DISK_CONDITIONER_IOC_SET) reset delay");
317*1b191cb5SApple OSS Distributions
318*1b191cb5SApple OSS Distributions usleep(USEC_PER_SEC / 2); // might still be other I/O inflight
319*1b191cb5SApple OSS Distributions elapsed_nsec = time_for_read(fd, buf);
320*1b191cb5SApple OSS Distributions T_ASSERT_LT_ULLONG(elapsed_nsec, expected_nsec, "After disabling DMC read(%ld) from %s is reasonably fast", READSIZE, path);
321*1b191cb5SApple OSS Distributions }
322*1b191cb5SApple OSS Distributions
323*1b191cb5SApple OSS Distributions #else /* TEST_UNENTITLED */
324*1b191cb5SApple OSS Distributions
325*1b191cb5SApple OSS Distributions #pragma mark Unentitled Tests
326*1b191cb5SApple OSS Distributions
327*1b191cb5SApple OSS Distributions T_DECL(fsctl_get_unentitled,
328*1b191cb5SApple OSS Distributions "fsctl.get should not require entitlement")
329*1b191cb5SApple OSS Distributions {
330*1b191cb5SApple OSS Distributions int err;
331*1b191cb5SApple OSS Distributions char *mount_path;
332*1b191cb5SApple OSS Distributions disk_conditioner_info info;
333*1b191cb5SApple OSS Distributions
334*1b191cb5SApple OSS Distributions T_SETUPBEGIN;
335*1b191cb5SApple OSS Distributions mount_path = mktempmount();
336*1b191cb5SApple OSS Distributions T_SETUPEND;
337*1b191cb5SApple OSS Distributions
338*1b191cb5SApple OSS Distributions err = fsctl(mount_path, DISK_CONDITIONER_IOC_GET, &info, 0);
339*1b191cb5SApple OSS Distributions T_WITH_ERRNO;
340*1b191cb5SApple OSS Distributions T_ASSERT_EQ_INT(0, err, "fsctl.get without entitlement");
341*1b191cb5SApple OSS Distributions }
342*1b191cb5SApple OSS Distributions
343*1b191cb5SApple OSS Distributions T_DECL(fsctl_set_unentitled,
344*1b191cb5SApple OSS Distributions "fsctl.set should require entitlement")
345*1b191cb5SApple OSS Distributions {
346*1b191cb5SApple OSS Distributions int err;
347*1b191cb5SApple OSS Distributions char *mount_path;
348*1b191cb5SApple OSS Distributions disk_conditioner_info info = {0};
349*1b191cb5SApple OSS Distributions disk_conditioner_info expected_info = {0};
350*1b191cb5SApple OSS Distributions
351*1b191cb5SApple OSS Distributions T_SETUPBEGIN;
352*1b191cb5SApple OSS Distributions mount_path = mktempmount();
353*1b191cb5SApple OSS Distributions T_SETUPEND;
354*1b191cb5SApple OSS Distributions
355*1b191cb5SApple OSS Distributions // save original info
356*1b191cb5SApple OSS Distributions err = fsctl(mount_path, DISK_CONDITIONER_IOC_GET, &expected_info, 0);
357*1b191cb5SApple OSS Distributions T_WITH_ERRNO;
358*1b191cb5SApple OSS Distributions T_ASSERT_EQ_INT(0, err, "Get original DMC info");
359*1b191cb5SApple OSS Distributions
360*1b191cb5SApple OSS Distributions info.enabled = 1;
361*1b191cb5SApple OSS Distributions info.access_time_usec = 10;
362*1b191cb5SApple OSS Distributions err = fsctl(mount_path, DISK_CONDITIONER_IOC_SET, &info, 0);
363*1b191cb5SApple OSS Distributions T_WITH_ERRNO;
364*1b191cb5SApple OSS Distributions T_ASSERT_NE_INT(0, err, "fsctl.set returns error without entitlement");
365*1b191cb5SApple OSS Distributions
366*1b191cb5SApple OSS Distributions err = fsctl(mount_path, DISK_CONDITIONER_IOC_GET, &info, 0);
367*1b191cb5SApple OSS Distributions T_WITH_ERRNO;
368*1b191cb5SApple OSS Distributions T_ASSERT_EQ_INT(0, err, "fsctl.get after unentitled fsctl.set");
369*1b191cb5SApple OSS Distributions
370*1b191cb5SApple OSS Distributions err = memcmp(&info, &expected_info, sizeof(info));
371*1b191cb5SApple OSS Distributions T_ASSERT_EQ_INT(0, err, "fsctl.set should not change info without entitlement");
372*1b191cb5SApple OSS Distributions }
373*1b191cb5SApple OSS Distributions
374*1b191cb5SApple OSS Distributions #endif /* TEST_UNENTITLED */
375*1b191cb5SApple OSS Distributions
376*1b191cb5SApple OSS Distributions #pragma mark Helpers
377*1b191cb5SApple OSS Distributions
378*1b191cb5SApple OSS Distributions static char *
mktempdir(void)379*1b191cb5SApple OSS Distributions mktempdir(void)
380*1b191cb5SApple OSS Distributions {
381*1b191cb5SApple OSS Distributions char *path = malloc(PATH_MAX);
382*1b191cb5SApple OSS Distributions strcpy(path, "/tmp/dmc.XXXXXXXX");
383*1b191cb5SApple OSS Distributions atexit_b(^{ free(path); });
384*1b191cb5SApple OSS Distributions
385*1b191cb5SApple OSS Distributions // create a temporary mount to run the fsctl on
386*1b191cb5SApple OSS Distributions T_WITH_ERRNO;
387*1b191cb5SApple OSS Distributions T_ASSERT_NOTNULL(mkdtemp(path), "Create temporary directory");
388*1b191cb5SApple OSS Distributions atexit_b(^{ remove(path); });
389*1b191cb5SApple OSS Distributions
390*1b191cb5SApple OSS Distributions return path;
391*1b191cb5SApple OSS Distributions }
392*1b191cb5SApple OSS Distributions
393*1b191cb5SApple OSS Distributions /*
394*1b191cb5SApple OSS Distributions * Return the path to a temporary mount
395*1b191cb5SApple OSS Distributions * with no usable filesystem but still
396*1b191cb5SApple OSS Distributions * can be configured by the disk conditioner
397*1b191cb5SApple OSS Distributions *
398*1b191cb5SApple OSS Distributions * Faster than creating a ram disk to test with
399*1b191cb5SApple OSS Distributions * when access to the filesystem is not necessary
400*1b191cb5SApple OSS Distributions */
401*1b191cb5SApple OSS Distributions static char *
mktempmount(void)402*1b191cb5SApple OSS Distributions mktempmount(void)
403*1b191cb5SApple OSS Distributions {
404*1b191cb5SApple OSS Distributions char *mount_path = mktempdir();
405*1b191cb5SApple OSS Distributions
406*1b191cb5SApple OSS Distributions T_WITH_ERRNO;
407*1b191cb5SApple OSS Distributions T_ASSERT_EQ_INT(0, mount("devfs", mount_path, MNT_RDONLY, NULL), "Create temporary devfs mount");
408*1b191cb5SApple OSS Distributions atexit_b(^{ unmount(mount_path, MNT_FORCE); });
409*1b191cb5SApple OSS Distributions
410*1b191cb5SApple OSS Distributions return mount_path;
411*1b191cb5SApple OSS Distributions }
412*1b191cb5SApple OSS Distributions
413*1b191cb5SApple OSS Distributions #ifndef TEST_UNENTITLED
414*1b191cb5SApple OSS Distributions
415*1b191cb5SApple OSS Distributions /*
416*1b191cb5SApple OSS Distributions * Wrapper around dt_launch_tool/dt_waitpid
417*1b191cb5SApple OSS Distributions * that works like libc:system()
418*1b191cb5SApple OSS Distributions */
419*1b191cb5SApple OSS Distributions static int
system_legal(const char * command)420*1b191cb5SApple OSS Distributions system_legal(const char *command)
421*1b191cb5SApple OSS Distributions {
422*1b191cb5SApple OSS Distributions pid_t pid = -1;
423*1b191cb5SApple OSS Distributions int exit_status = 0;
424*1b191cb5SApple OSS Distributions const char *argv[] = {
425*1b191cb5SApple OSS Distributions _PATH_BSHELL,
426*1b191cb5SApple OSS Distributions "-c",
427*1b191cb5SApple OSS Distributions command,
428*1b191cb5SApple OSS Distributions NULL
429*1b191cb5SApple OSS Distributions };
430*1b191cb5SApple OSS Distributions
431*1b191cb5SApple OSS Distributions int rc = dt_launch_tool(&pid, (char **)(void *)argv, false, NULL, NULL);
432*1b191cb5SApple OSS Distributions if (rc != 0) {
433*1b191cb5SApple OSS Distributions return -1;
434*1b191cb5SApple OSS Distributions }
435*1b191cb5SApple OSS Distributions if (!dt_waitpid(pid, &exit_status, NULL, 30)) {
436*1b191cb5SApple OSS Distributions if (exit_status != 0) {
437*1b191cb5SApple OSS Distributions return exit_status;
438*1b191cb5SApple OSS Distributions }
439*1b191cb5SApple OSS Distributions return -1;
440*1b191cb5SApple OSS Distributions }
441*1b191cb5SApple OSS Distributions
442*1b191cb5SApple OSS Distributions return exit_status;
443*1b191cb5SApple OSS Distributions }
444*1b191cb5SApple OSS Distributions
445*1b191cb5SApple OSS Distributions /*
446*1b191cb5SApple OSS Distributions * Return the path to a temporary mount
447*1b191cb5SApple OSS Distributions * that contains a usable HFS+ filesystem
448*1b191cb5SApple OSS Distributions * mounted via a ram disk
449*1b191cb5SApple OSS Distributions */
450*1b191cb5SApple OSS Distributions static char *
mkramdisk(void)451*1b191cb5SApple OSS Distributions mkramdisk(void)
452*1b191cb5SApple OSS Distributions {
453*1b191cb5SApple OSS Distributions char cmd[1024];
454*1b191cb5SApple OSS Distributions char *mount_path = mktempdir();
455*1b191cb5SApple OSS Distributions char *dev_disk_file = malloc(256);
456*1b191cb5SApple OSS Distributions atexit_b(^{ free(dev_disk_file); });
457*1b191cb5SApple OSS Distributions strcpy(dev_disk_file, "/tmp/dmc.ramdisk.XXXXXXXX");
458*1b191cb5SApple OSS Distributions
459*1b191cb5SApple OSS Distributions T_WITH_ERRNO;
460*1b191cb5SApple OSS Distributions T_ASSERT_NOTNULL(mktemp(dev_disk_file), "Create temporary file to store dev disk for ramdisk");
461*1b191cb5SApple OSS Distributions atexit_b(^{ remove(dev_disk_file); });
462*1b191cb5SApple OSS Distributions
463*1b191cb5SApple OSS Distributions // create the RAM disk device
464*1b191cb5SApple OSS Distributions snprintf(cmd, sizeof(cmd), "hdik -nomount ram://10000 > %s", dev_disk_file);
465*1b191cb5SApple OSS Distributions T_ASSERT_EQ_INT(0, system_legal(cmd), "Create ramdisk");
466*1b191cb5SApple OSS Distributions
467*1b191cb5SApple OSS Distributions atexit_b(^{
468*1b191cb5SApple OSS Distributions char eject_cmd[1024];
469*1b191cb5SApple OSS Distributions unmount(mount_path, MNT_FORCE);
470*1b191cb5SApple OSS Distributions snprintf(eject_cmd, sizeof(eject_cmd), "hdik -e `cat %s`", dev_disk_file);
471*1b191cb5SApple OSS Distributions system_legal(eject_cmd);
472*1b191cb5SApple OSS Distributions remove(dev_disk_file);
473*1b191cb5SApple OSS Distributions });
474*1b191cb5SApple OSS Distributions
475*1b191cb5SApple OSS Distributions // initialize as an HFS volume
476*1b191cb5SApple OSS Distributions snprintf(cmd, sizeof(cmd), "newfs_hfs `cat %s`", dev_disk_file);
477*1b191cb5SApple OSS Distributions T_ASSERT_EQ_INT(0, system_legal(cmd), "Initialize ramdisk as HFS");
478*1b191cb5SApple OSS Distributions
479*1b191cb5SApple OSS Distributions // mount it
480*1b191cb5SApple OSS Distributions snprintf(cmd, sizeof(cmd), "mount -t hfs `cat %s` %s", dev_disk_file, mount_path);
481*1b191cb5SApple OSS Distributions T_ASSERT_EQ_INT(0, system_legal(cmd), "Mount ramdisk");
482*1b191cb5SApple OSS Distributions
483*1b191cb5SApple OSS Distributions return mount_path;
484*1b191cb5SApple OSS Distributions }
485*1b191cb5SApple OSS Distributions
486*1b191cb5SApple OSS Distributions static uint64_t
time_for_read(int fd,const char * expected)487*1b191cb5SApple OSS Distributions time_for_read(int fd, const char *expected)
488*1b191cb5SApple OSS Distributions {
489*1b191cb5SApple OSS Distributions int err;
490*1b191cb5SApple OSS Distributions ssize_t ret;
491*1b191cb5SApple OSS Distributions char buf[READSIZE];
492*1b191cb5SApple OSS Distributions uint64_t start, stop;
493*1b191cb5SApple OSS Distributions
494*1b191cb5SApple OSS Distributions bzero(buf, sizeof(buf));
495*1b191cb5SApple OSS Distributions lseek(fd, 0, SEEK_SET);
496*1b191cb5SApple OSS Distributions
497*1b191cb5SApple OSS Distributions start = dt_nanoseconds();
498*1b191cb5SApple OSS Distributions ret = read(fd, buf, READSIZE);
499*1b191cb5SApple OSS Distributions stop = dt_nanoseconds();
500*1b191cb5SApple OSS Distributions
501*1b191cb5SApple OSS Distributions T_ASSERT_GE_LONG(ret, 0L, "read from temporary file");
502*1b191cb5SApple OSS Distributions T_ASSERT_EQ_LONG(ret, READSIZE, "read %ld bytes from temporary file", READSIZE);
503*1b191cb5SApple OSS Distributions err = memcmp(buf, expected, sizeof(buf));
504*1b191cb5SApple OSS Distributions T_ASSERT_EQ_INT(0, err, "read expected contents from temporary file");
505*1b191cb5SApple OSS Distributions
506*1b191cb5SApple OSS Distributions return stop - start;
507*1b191cb5SApple OSS Distributions }
508*1b191cb5SApple OSS Distributions
509*1b191cb5SApple OSS Distributions static void
perf_setup(char ** path,int * fd)510*1b191cb5SApple OSS Distributions perf_setup(char **path, int *fd)
511*1b191cb5SApple OSS Distributions {
512*1b191cb5SApple OSS Distributions int temp_fd;
513*1b191cb5SApple OSS Distributions char *temp_path;
514*1b191cb5SApple OSS Distributions
515*1b191cb5SApple OSS Distributions char *mount_path = mkramdisk();
516*1b191cb5SApple OSS Distributions temp_path = *path = malloc(PATH_MAX);
517*1b191cb5SApple OSS Distributions snprintf(temp_path, PATH_MAX, "%s/dmc.XXXXXXXX", mount_path);
518*1b191cb5SApple OSS Distributions atexit_b(^{ free(temp_path); });
519*1b191cb5SApple OSS Distributions
520*1b191cb5SApple OSS Distributions T_ASSERT_NOTNULL(mktemp(temp_path), "Create temporary file");
521*1b191cb5SApple OSS Distributions atexit_b(^{ remove(temp_path); });
522*1b191cb5SApple OSS Distributions
523*1b191cb5SApple OSS Distributions temp_fd = *fd = open(temp_path, O_RDWR | O_CREAT);
524*1b191cb5SApple OSS Distributions T_WITH_ERRNO;
525*1b191cb5SApple OSS Distributions T_ASSERT_GE_INT(temp_fd, 0, "Open temporary file for read/write");
526*1b191cb5SApple OSS Distributions atexit_b(^{ close(temp_fd); });
527*1b191cb5SApple OSS Distributions fcntl(temp_fd, F_NOCACHE, 1);
528*1b191cb5SApple OSS Distributions }
529*1b191cb5SApple OSS Distributions #endif /* !TEST_UNENTITLED */
530