Path: blob/main/tests/sys/cam/ctl/prout_register_huge_cdb.c
96309 views
/*1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2024 ConnectWise4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS DOCUMENTATION IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR16* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES17* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.18* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,19* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT20* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,21* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY22* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT23* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF24* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.25*/2627/*28* Helper that sends a PERSISTENT RESERVATION OUT command to CTL with a29* ridiculously huge size for the length of the CDB. This is not possible with30* ctladm, for good reason.31*/32#include <camlib.h>33#include <err.h>34#include <fcntl.h>35#include <stdio.h>36#include <stdlib.h>37#include <string.h>38#include <sys/ioctl.h>3940#include <cam/scsi/scsi_message.h>41#include <cam/ctl/ctl_io.h>42#include <cam/ctl/ctl.h>43#include <cam/ctl/ctl_ioctl.h>44#include <cam/ctl/ctl_util.h>4546int47main(int argc, char **argv)48{49union ctl_io *io;50int fd = open("/dev/cam/ctl", O_RDWR);51int r;52uint32_t targ_port;5354if (argc < 2)55errx(2, "usage: prout_register_huge_cdb <target_port>\n");5657targ_port = strtoul(argv[1], NULL, 10);5859io = calloc(1, sizeof(*io));60io->io_hdr.nexus.initid = 7; /* 7 is ctladm's default initiator id */61io->io_hdr.nexus.targ_port = targ_port;62io->io_hdr.nexus.targ_mapped_lun = 0;63io->io_hdr.nexus.targ_lun = 0;64io->io_hdr.io_type = CTL_IO_SCSI;65io->taskio.tag_type = CTL_TAG_UNTAGGED;66uint8_t cdb[32] = {};67// ctl_persistent_reserve_out// 5f 0068cdb[0] = 0x5f;69cdb[1] = 0x00;70struct scsi_per_res_out *cdb_ = ( struct scsi_per_res_out *)cdb;71// Claim an enormous size of the CDB, but don't actually alloc it all.72cdb_->length[0] = 0xff;73cdb_->length[1] = 0xff;74cdb_->length[2] = 0xff;75cdb_->length[3] = 0xff;76io->scsiio.cdb_len = sizeof(cdb);77memcpy(io->scsiio.cdb, cdb, sizeof(cdb));78io->io_hdr.flags |= CTL_FLAG_DATA_IN;79r = ioctl(fd, CTL_IO, io);80if (r == -1)81err(1, "ioctl");82if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {83return (0);84} else {85return (1);86}87}888990