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 // OK, that should have created 1024 invalid ranges. Sync the data.
137 p = mmap(NULL, 1024 * ps * 2, PROT_READ | PROT_WRITE, MAP_SHARED,
139 assert(p != MAP_FAILED);
141 assert_no_err(msync(p, 1024 * ps * 2, MS_SYNC));
143 // Now sync the invalid ranges
144 assert_no_err(fcntl(fd, F_FULLFSYNC));
146 assert_no_err(close(fd));
148 assert_no_err(unlink(file));
150 #if !TARGET_OS_EMBEDDED
151 disk_image_t *di2 = disk_image_create(DISK_IMAGE, &(disk_image_opts_t){
152 .size = 100 * 1024 * 1024
155 // Find the diskimages_helper process
156 char *dev_device = strdup(di2->disk + 5);
158 // Strip off the s bit for the partition
159 char *spos = strrchr(dev_device, 's');
163 io_service_t obj = IOServiceGetMatchingService(kIOMasterPortDefault,
164 IOBSDNameMatching(kIOMasterPortDefault, 0, dev_device));
170 // obj should be the IOMedia object. Go up three to the IOHDIXHDDrive object.
171 assert(!IORegistryEntryGetParentEntry(obj, kIOServicePlane, &parent));
175 IOObjectRelease(obj);
177 assert(!IORegistryEntryGetParentEntry(parent, kIOServicePlane, &obj));
179 IOObjectRelease(parent);
181 assert(!IORegistryEntryGetParentEntry(obj, kIOServicePlane, &parent));
185 IOObjectRelease(obj);
187 NSString *creator = (id)CFBridgingRelease(IORegistryEntrySearchCFProperty(parent, kIOServicePlane,
188 CFSTR("IOUserClientCreator"),
190 kIORegistryIterateRecursively));
192 IOObjectRelease(parent);
196 // Extract the pid of disk_images_helper
197 pid_t disk_images_helper_pid;
198 assert(sscanf([creator UTF8String], "pid %u", &disk_images_helper_pid) == 1);
203 asprintf(&path, "%s/test-file", di2->mount_point);
205 fd = open(path, O_CREAT | O_RDWR, 0666);
208 * Workaround for <rdar://20688964>: force the journal to
209 * have at least one transaction in it.
211 assert_no_err(fcntl(fd, F_FULLFSYNC));
215 assert_no_err(fcntl(fd, F_NOCACHE, 1));
217 int block_size = 65536;
220 struct fstore fst = {
221 .fst_posmode = F_PEOFPOSMODE,
222 .fst_length = 2 * block_size,
225 assert_no_err(fcntl(fd, F_PREALLOCATE, &fst));
227 assert(fst.fst_bytesalloc >= 2 * block_size);
229 // Figure out where that is on the device
230 struct log2phys l2p = { .l2p_contigbytes = block_size, .l2p_devoffset = block_size };
231 assert_no_err(fcntl(fd, F_LOG2PHYS_EXT, &l2p));
233 assert(l2p.l2p_contigbytes > 0);
235 // Now open the raw device and write some garbage to that location
237 assert(!strncmp(di2->disk, "/dev/", 5));
240 asprintf(&raw_path, "/dev/r%s", di2->disk + 5);
242 int raw_dev = open(raw_path, O_RDWR);
244 assert_with_errno(raw_dev >= 0);
246 memset(buf, 0x57, block_size);
248 check_io(pwrite(raw_dev, buf, l2p.l2p_contigbytes, l2p.l2p_devoffset),
249 l2p.l2p_contigbytes);
251 assert_no_err(close(raw_dev));
253 // OK, so now we have some garbage where we want it.
255 // Check fcntl F_LOG2PHYS_EXT is doing what we expect
256 off_t file_offset = block_size;
258 assert(l2p.l2p_contigbytes > 0);
259 assert(l2p.l2p_contigbytes < 1024 * 1024);
260 file_offset += l2p.l2p_contigbytes;
261 assert(file_offset < 1024 * 1024);
262 l2p.l2p_devoffset = file_offset;
263 l2p.l2p_contigbytes = INT64_MAX;
264 } while (!fcntl(fd, F_LOG2PHYS_EXT, &l2p));
266 assert_with_errno(errno == ERANGE);
268 // Do some writing to the file normally
270 memset(buf, 0xaa, block_size);
272 check_io(pwrite(fd, buf, block_size, 0), block_size);
274 check_io(pwrite(fd, buf, block_size, block_size * 2), block_size);
276 // So now we have a hole that should be zeroed at <block_size, block_size>
278 // Touch every page in that hole
279 for (int i = 0; i < block_size / 4096; ++i)
280 check_io(pwrite(fd, "hello", 5, block_size + i * 4096 + 1000), 5);
282 // Check what we have in the cache
283 check_io(pread(fd, buf, ps, block_size), ps);
285 assert(!memcmp(buf, zero, 1000));
286 assert(!memcmp(buf + 1000, "hello", 5));
287 assert(!memcmp(buf + 1005, zero, ps - 1005));
289 /* Write something into the block beyond the hole. This should
290 cause a metadata update. */
291 check_io(pwrite(fd, "hello", 5, block_size * 3), 5);
293 /* Create another file so we can do a full fsync to
294 force a journal flush. */
296 asprintf(&fsync_path, "%s/fsync", di->mount_point);
298 fd = open(fsync_path, O_CREAT | O_RDWR | O_TRUNC, 0666);
300 assert_with_errno(fd >= 0);
302 assert_no_err(fcntl(fd, F_FULLFSYNC));
304 // Kill disk_images_helper to simulate a crash
305 assert_no_err(kill(disk_images_helper_pid, SIGKILL));
307 // Wait until it gets unmounted
309 while (!stat(raw_path, &sb))
312 // Wait another second for things to settle down
315 // Attach to the disk image again
316 assert(!systemx("/usr/bin/hdiutil", SYSTEMX_QUIET, "attach", DISK_IMAGE, NULL));
318 assert_with_errno((fd = open(path, O_RDWR)) >= 0);
320 // Either the file should be short, or there should be zeroes
321 ssize_t amount = pread(fd, buf, block_size, block_size);
322 assert_with_errno(amount >= 0);
324 assert(!memcmp(buf, zero, amount));
326 assert_no_err(close(fd));
330 // Test for <rdar://20994239>
331 fd = open(file, O_CREAT | O_RDWR, 0666);
332 assert_with_errno(fd >= 0);
333 assert_no_err(fcntl(fd, F_NOCACHE, 1));
335 void *buf2 = malloc(0x100000);
336 memset(buf2, 0x16, 0x100000);
337 check_io(pwrite(fd, buf2, 0x200, 0), 0x200);
338 check_io(pwrite(fd, buf2, 0x100000, 0x00100200), 0x100000);
339 check_io(pwrite(fd, buf2, 0x100000, 0x00300200), 0x100000);
340 check_io(pwrite(fd, buf2, 0x100000, 0x00500200), 0x100000);
341 check_io(pwrite(fd, buf2, 0x200, 0), 0x200);
342 check_io(pwrite(fd, buf2, 0x100000, 0x00700200), 0x100000);
343 check_io(pwrite(fd, buf2, 0x100000, 0x200), 0x100000);
345 void *buf3 = malloc(0x100000);
346 check_io(pread(fd, buf3, 0x100000, 0x100000), 0x100000);
347 assert(!memcmp(buf2, buf3, 0x100000));
349 assert_no_err(close(fd));
350 assert_no_err(unlink(file));