8 #include <TargetConditionals.h>
10 #import <Foundation/Foundation.h>
12 #include "hfs-tests.h"
13 #include "test-utils.h"
14 #include "disk-image.h"
19 static disk_image_t *di;
21 #define DISK_IMAGE "/tmp/invalid-ranges.sparseimage"
23 int run_invalid_ranges(__unused test_ctx_t *ctx)
25 di = disk_image_get();
28 asprintf(&file, "%s/invalid-ranges.data", di->mount_point);
32 int fd = open(file, O_CREAT | O_RDWR, 0666);
34 assert_with_errno(fd >= 0);
38 // Make a big file and punch holes in it
41 void *buf = valloc(65536);
43 memset(buf, 0xaf, 65536);
45 assert_no_err(fcntl(fd, F_NOCACHE, 1));
48 for (o = 0; o < size; o += 131072)
49 check_io(pwrite(fd, buf, 65536, o + 65536), 65536);
51 // This should cause everything to be flushed
52 assert_no_err(close(fd));
54 assert_with_errno((fd = open(file, O_RDWR)) >= 0);
57 assert_with_errno((p = mmap(NULL, o, PROT_READ | PROT_WRITE, MAP_SHARED,
58 fd, 0)) != MAP_FAILED);
60 assert_no_err(msync(p, o, MS_INVALIDATE));
62 void *zero = malloc(65536);
68 for (n = 0, q = p; n < o; n += 131072, q += 131072) {
69 assert(!memcmp(q, zero, 65536));
70 assert(!memcmp(q + 65536, buf, 65536));
73 assert(p[size] == 0xaf);
75 assert_no_err(ftruncate(fd, size));
77 // Check the tail portion of the page is zero
82 msync(p + size - 1, 1, MS_SYNC);
84 int ps = getpagesize();
88 assert_no_err(msync((void *)((uintptr_t)(p + size - 1) & ~ps_mask),
91 // Page back in and check it's zeroed
96 // Extend the file to include the change we made above
97 assert_no_err(ftruncate(fd, size + 1));
99 // That change should have been zeroed out
100 assert(p[size] == 0);
102 assert_no_err(munmap(p, o));
105 assert_no_err(ftruncate(fd, o + 2 * ps));
107 // Write something into the middle of the page following o
108 off_t hello_offset = roundup(o, ps) + 100;
110 check_io(pwrite(fd, "hello", 5, hello_offset), 5);
113 assert_no_err(close(fd));
115 assert_with_errno((fd = open(file, O_RDWR)) >= 0);
117 assert_with_errno((p = mmap(NULL, o + 2 * ps, PROT_READ | PROT_WRITE, MAP_SHARED,
118 fd, 0)) != MAP_FAILED);
120 assert_no_err(msync(p, o + 2 * ps, MS_INVALIDATE));
122 assert(!memcmp(p + hello_offset, "hello", 5));
123 assert(!memcmp(p + size, zero, hello_offset - size));
124 assert(!memcmp(p + hello_offset + 5, zero, o + ps * 2 - hello_offset - 5));
126 assert_no_err(close(fd));
127 assert_no_err(unlink(file));
129 // Make a large number of invalid ranges
130 assert_with_errno((fd = open(file,
131 O_RDWR | O_CREAT, 0666)) >= 0);
132 for (int i = 0; i < 1024; ++i) {
133 pwrite(fd, "hello", 5, i * ps * 2);
136 assert_no_err(munmap(p, o + 2 * ps));
138 // OK, that should have created 1024 invalid ranges. Sync the data.
139 p = mmap(NULL, 1024 * ps * 2, PROT_READ | PROT_WRITE, MAP_SHARED,
141 assert(p != MAP_FAILED);
143 assert_no_err(msync(p, 1024 * ps * 2, MS_SYNC));
145 // Now sync the invalid ranges
146 assert_no_err(fcntl(fd, F_FULLFSYNC));
148 assert_no_err(close(fd));
150 assert_no_err(unlink(file));
152 assert_no_err(munmap(p, 1024 * ps * 2));
154 #if !TARGET_OS_EMBEDDED
155 disk_image_t *di2 = disk_image_create(DISK_IMAGE, &(disk_image_opts_t){
156 .size = 100 * 1024 * 1024
159 // Find the diskimages_helper process
160 char *dev_device = strdup(di2->disk + 5);
162 // Strip off the s bit for the partition
163 char *spos = strrchr(dev_device, 's');
167 io_service_t obj = IOServiceGetMatchingService(kIOMasterPortDefault,
168 IOBSDNameMatching(kIOMasterPortDefault, 0, dev_device));
174 // obj should be the IOMedia object. Go up three to the IOHDIXHDDrive object.
175 assert(!IORegistryEntryGetParentEntry(obj, kIOServicePlane, &parent));
179 IOObjectRelease(obj);
181 assert(!IORegistryEntryGetParentEntry(parent, kIOServicePlane, &obj));
183 IOObjectRelease(parent);
185 assert(!IORegistryEntryGetParentEntry(obj, kIOServicePlane, &parent));
189 IOObjectRelease(obj);
191 NSString *creator = (id)CFBridgingRelease(IORegistryEntrySearchCFProperty(parent, kIOServicePlane,
192 CFSTR("IOUserClientCreator"),
194 kIORegistryIterateRecursively));
196 IOObjectRelease(parent);
200 // Extract the pid of disk_images_helper
201 pid_t disk_images_helper_pid;
202 assert(sscanf([creator UTF8String], "pid %u", &disk_images_helper_pid) == 1);
207 asprintf(&path, "%s/test-file", di2->mount_point);
209 fd = open(path, O_CREAT | O_RDWR, 0666);
212 * Workaround for <rdar://20688964>: force the journal to
213 * have at least one transaction in it.
215 assert_no_err(fcntl(fd, F_FULLFSYNC));
219 assert_no_err(fcntl(fd, F_NOCACHE, 1));
221 int block_size = 65536;
224 struct fstore fst = {
225 .fst_posmode = F_PEOFPOSMODE,
226 .fst_length = 2 * block_size,
229 assert_no_err(fcntl(fd, F_PREALLOCATE, &fst));
231 assert(fst.fst_bytesalloc >= 2 * block_size);
233 // Figure out where that is on the device
234 struct log2phys l2p = { .l2p_contigbytes = block_size, .l2p_devoffset = block_size };
235 assert_no_err(fcntl(fd, F_LOG2PHYS_EXT, &l2p));
237 assert(l2p.l2p_contigbytes > 0);
239 // Now open the raw device and write some garbage to that location
241 assert(!strncmp(di2->disk, "/dev/", 5));
244 asprintf(&raw_path, "/dev/r%s", di2->disk + 5);
246 int raw_dev = open(raw_path, O_RDWR);
248 assert_with_errno(raw_dev >= 0);
250 memset(buf, 0x57, block_size);
252 check_io(pwrite(raw_dev, buf, l2p.l2p_contigbytes, l2p.l2p_devoffset),
253 l2p.l2p_contigbytes);
255 assert_no_err(close(raw_dev));
257 // OK, so now we have some garbage where we want it.
259 // Check fcntl F_LOG2PHYS_EXT is doing what we expect
260 off_t file_offset = block_size;
262 assert(l2p.l2p_contigbytes > 0);
263 assert(l2p.l2p_contigbytes < 1024 * 1024);
264 file_offset += l2p.l2p_contigbytes;
265 assert(file_offset < 1024 * 1024);
266 l2p.l2p_devoffset = file_offset;
267 l2p.l2p_contigbytes = INT64_MAX;
268 } while (!fcntl(fd, F_LOG2PHYS_EXT, &l2p));
270 assert_with_errno(errno == ERANGE);
272 // Do some writing to the file normally
274 memset(buf, 0xaa, block_size);
276 check_io(pwrite(fd, buf, block_size, 0), block_size);
278 check_io(pwrite(fd, buf, block_size, block_size * 2), block_size);
280 // So now we have a hole that should be zeroed at <block_size, block_size>
282 // Touch every page in that hole
283 for (int i = 0; i < block_size / 4096; ++i)
284 check_io(pwrite(fd, "hello", 5, block_size + i * 4096 + 1000), 5);
286 // Check what we have in the cache
287 check_io(pread(fd, buf, ps, block_size), ps);
289 assert(!memcmp(buf, zero, 1000));
290 assert(!memcmp(buf + 1000, "hello", 5));
291 assert(!memcmp(buf + 1005, zero, ps - 1005));
293 /* Write something into the block beyond the hole. This should
294 cause a metadata update. */
295 check_io(pwrite(fd, "hello", 5, block_size * 3), 5);
297 /* Create another file so we can do a full fsync to
298 force a journal flush. */
300 asprintf(&fsync_path, "%s/fsync", di->mount_point);
302 assert_no_err(close(fd));
303 fd = open(fsync_path, O_CREAT | O_RDWR | O_TRUNC, 0666);
305 assert_with_errno(fd >= 0);
307 assert_no_err(fcntl(fd, F_FULLFSYNC));
309 // Kill disk_images_helper to simulate a crash
310 assert_no_err(kill(disk_images_helper_pid, SIGKILL));
312 // Wait until it gets unmounted
314 while (!stat(raw_path, &sb))
317 // Wait another second for things to settle down
320 // Attach to the disk image again
321 assert(!systemx("/usr/bin/hdiutil", SYSTEMX_QUIET, "attach", DISK_IMAGE, NULL));
323 assert_no_err(close(fd));
324 assert_with_errno((fd = open(path, O_RDWR)) >= 0);
326 // Either the file should be short, or there should be zeroes
327 ssize_t amount = pread(fd, buf, block_size, block_size);
328 assert_with_errno(amount >= 0);
330 assert(!memcmp(buf, zero, (amount > 1000) ? 1000 : amount));
332 assert_no_err(close(fd));
336 // Test for <rdar://20994239>
337 fd = open(file, O_CREAT | O_RDWR, 0666);
338 assert_with_errno(fd >= 0);
339 assert_no_err(fcntl(fd, F_NOCACHE, 1));
341 void *buf2 = malloc(0x100000);
342 memset(buf2, 0x16, 0x100000);
343 check_io(pwrite(fd, buf2, 0x200, 0), 0x200);
344 check_io(pwrite(fd, buf2, 0x100000, 0x00100200), 0x100000);
345 check_io(pwrite(fd, buf2, 0x100000, 0x00300200), 0x100000);
346 check_io(pwrite(fd, buf2, 0x100000, 0x00500200), 0x100000);
347 check_io(pwrite(fd, buf2, 0x200, 0), 0x200);
348 check_io(pwrite(fd, buf2, 0x100000, 0x00700200), 0x100000);
349 check_io(pwrite(fd, buf2, 0x100000, 0x200), 0x100000);
351 void *buf3 = malloc(0x100000);
352 check_io(pread(fd, buf3, 0x100000, 0x100000), 0x100000);
353 assert(!memcmp(buf2, buf3, 0x100000));
359 assert_no_err(close(fd));
360 assert_no_err(unlink(file));