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