+/*
+ * vnode_trim:
+ * Used to call the DKIOCUNMAP ioctl on the underlying disk device for the specified vnode.
+ * Trims the region at offset bytes into the file, for length bytes.
+ *
+ * Care must be taken to ensure that the vnode is sufficiently reference counted at the time this
+ * function is called; no iocounts or usecounts are taken on the vnode.
+ * This function is non-idempotent in error cases; We cannot un-discard the blocks if only some of them
+ * are successfully discarded.
+ */
+u_int32_t vnode_trim (
+ struct vnode *vp,
+ off_t offset,
+ size_t length)
+{
+ daddr64_t io_blockno; /* Block number corresponding to the start of the extent */
+ size_t io_bytecount; /* Number of bytes in current extent for the specified range */
+ size_t trimmed = 0;
+ off_t current_offset = offset;
+ size_t remaining_length = length;
+ int error = 0;
+ u_int32_t blocksize = 0;
+ struct vnode *devvp;
+ dk_extent_t extent;
+ dk_unmap_t unmap;
+
+
+ /* Get the underlying device vnode */
+ devvp = vp->v_mount->mnt_devvp;
+
+ /* Figure out the underlying device block size */
+ error = VNOP_IOCTL(devvp, DKIOCGETBLOCKSIZE, (caddr_t)&blocksize, 0, vfs_context_kernel());
+ if (error) {
+ goto trim_exit;
+ }
+
+ /*
+ * We may not get the entire range from offset -> offset+length in a single
+ * extent from the blockmap call. Keep looping/going until we are sure we've hit
+ * the whole range or if we encounter an error.
+ */
+ while (trimmed < length) {
+ /*
+ * VNOP_BLOCKMAP will tell us the logical to physical block number mapping for the
+ * specified offset. It returns blocks in contiguous chunks, so if the logical range is
+ * broken into multiple extents, it must be called multiple times, increasing the offset
+ * in each call to ensure that the entire range is covered.
+ */
+ error = VNOP_BLOCKMAP (vp, current_offset, remaining_length,
+ &io_blockno, &io_bytecount, NULL, VNODE_READ, NULL);
+
+ if (error) {
+ goto trim_exit;
+ }
+ /*
+ * We have a contiguous run. Prepare & issue the ioctl for the device.
+ * the DKIOCUNMAP ioctl takes offset in bytes from the start of the device.
+ */
+ memset (&extent, 0, sizeof(dk_extent_t));
+ memset (&unmap, 0, sizeof(dk_unmap_t));
+ extent.offset = (uint64_t) io_blockno * (u_int64_t) blocksize;
+ extent.length = io_bytecount;
+ unmap.extents = &extent;
+ unmap.extentsCount = 1;
+ error = VNOP_IOCTL(devvp, DKIOCUNMAP, (caddr_t)&unmap, 0, vfs_context_kernel());
+
+ if (error) {
+ goto trim_exit;
+ }
+ remaining_length = remaining_length - io_bytecount;
+ trimmed = trimmed + io_bytecount;
+ current_offset = current_offset + io_bytecount;
+ }
+trim_exit:
+
+ return error;
+
+}
+